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


Java Scanner useDelimiter()用法及代碼示例


useDelimiter(Pattern pattern)

java.util.Scanner類的useDelimiter(Pattern pattern)方法將此掃描程序的定界模式設置為指定的模式。

用法:

public Scanner useDelimiter(Pattern pattern)

參數:該函數接受一個強製性的參數模式,該模式指定一個定界模式。


返回值:函數返回此掃描儀。

以下示例程序旨在說明上述函數:

示例1:

// Java program to illustrate the useDelimiter() 
// 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(); 
    } 
}
輸出:
Scanner String: 
Geeksforgeeks has Scanner Class Methods
Old delimiter: \p{javaWhitespace}+
New delimiter: .ll.

useDelimiter(String pattern)

java.util.Scanner類的useDelimiter(String pattern)方法將此掃描儀的定界模式設置為從指定String構造的模式。

用法:

public Scanner useDelimiter(String pattern)

參數:該函數接受強製性的參數字符串模式,該模式指定了定界模式。

返回值:函數返回此掃描儀。

以下示例程序旨在說明上述函數:

示例1:

// Java program to illustrate the useDelimter() 
// 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://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)



相關用法


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