Java中的 Scanner useDelimiter() 方法及示例
useDelimiter(模式模式)
Java.util.Scanner类的useDelimiter(Pattern pattern)方法将此扫描仪的分隔模式设置为指定的模式。
句法:
public Scanner useDelimiter(Pattern pattern)
参数:该函数接受指定分隔模式的强制参数模式。
返回值:该函数返回此扫描仪。
下面的程序说明了上述函数:
方案一:
Java
// Java program to illustrate the useDelimiter(Pattern Pattern)
// method of Scanner class in Java
import java.util.*;
import java.util.regex.Pattern;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Geeksforgeeks has Scanner Class Methods";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// print a line of the scanner
System.out.println("Scanner String: \n"
+ scanner.nextLine());
// display the old delimiter
System.out.println("Old delimiter: "
+ scanner.delimiter());
// change the delimiter of this scanner
scanner.useDelimiter(Pattern.compile(".ll."));
// display the new delimiter
System.out.println("New delimiter: "
+ scanner.delimiter());
// close the scanner
scanner.close();
}
}
Java
// Java program to illustrate the useDelimter(String Pattern)
// method of Scanner class in Java
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Geeksforgeeks has Scanner Class Methods";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// print a line of the scanner
System.out.println("Scanner String: \n"
+ scanner.nextLine());
// print the old delimiter
System.out.println("Old Delimiter: "
+ scanner.delimiter());
// change the delimiter
scanner.useDelimiter("Wor");
// print the new delimiter
System.out.println("New Delimiter: "
+ scanner.delimiter());
// close the scanner
scanner.close();
}
}
输出:
Scanner String:
Geeksforgeeks has Scanner Class Methods
Old delimiter: \p{javaWhitespace}+
New delimiter: .ll.
使用分隔符(字符串模式)
Java.util.Scanner类的useDelimiter(String pattern)方法 将此扫描器的分隔模式设置为从指定 String 构造的模式。
句法:
public Scanner useDelimiter(String pattern)
参数:该函数接受指定分隔模式的强制参数字符串模式。
返回值:该函数返回此扫描仪。
下面的程序说明了上述函数:
方案一:
Java
// Java program to illustrate the useDelimter(String Pattern)
// method of Scanner class in Java
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
String s = "Geeksforgeeks has Scanner Class Methods";
// create a new scanner
// with the specified String Object
Scanner scanner = new Scanner(s);
// print a line of the scanner
System.out.println("Scanner String: \n"
+ scanner.nextLine());
// print the old delimiter
System.out.println("Old Delimiter: "
+ scanner.delimiter());
// change the delimiter
scanner.useDelimiter("Wor");
// print the new delimiter
System.out.println("New Delimiter: "
+ scanner.delimiter());
// close the scanner
scanner.close();
}
}
输出:
Scanner String:
Geeksforgeeks has Scanner Class Methods
Old Delimiter: \p{javaWhitespace}+
New Delimiter: Wor
参考: https: Java Java.lang.String)