當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Scanner delimiter() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。