Java Scanner delimiter()方法及示例

Java Scanner delimiter()方法及示例

java.util.Scanner 类的 delimiter() 方法返回该Scanner当前用于匹配分隔符的Pattern。

语法

public Pattern delimiter()

返回值: 该函数返回扫描仪的定界模式。

下面的程序说明了上述函数。

程序1 :

// Java program to illustrate the
// delimiter() 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);
  
        // prints the next line of the string
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
  
        // print the delimiter this scanner is using
        System.out.println("\nDelimiter being used in Scanner: "
                           + scanner.delimiter());
  
        // Close the scanner
        scanner.close();
    }
}

输出:

Scanner String: 
Geeksforgeeks has Scanner Class Methods

Delimiter being used in Scanner: \p{javaWhitespace}+

程序2

// Java program to illustrate the
// delimiter() 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);
  
        // Set the delimiter to "."
        scanner.useDelimiter(".");
  
        // prints the next line of the string
        System.out.println("Scanner String: \n"
                           + scanner.nextLine());
  
        // print the delimiter this scanner is using
        System.out.println("\nDelimiter being used in Scanner: "
                           + scanner.delimiter());
  
        // Close the scanner
        scanner.close();
    }
}

输出:

Scanner String: 
Geeksforgeeks.has.Scanner.Class.Methods

Delimiter being used in Scanner: .

**参考资料: ** https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#delimiter()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程