Scanner useDelimiter() method in Java with Examples
useDelimiter(Pattern pattern)
The useDelimiter(Pattern pattern) method of java.util.Scanner class sets this scanner’s delimiting pattern to the specified pattern.
Syntax:
public Scanner useDelimiter(Pattern pattern)
Parameter: The function accepts a mandatory parameter pattern which specifies a delimiting pattern.
Return Value: The function returns this scanner.
Below programs illustrate the above function:
Program 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)
The useDelimiter(String pattern) method of java.util.Scanner class Sets this scanner’s delimiting pattern to a pattern constructed from the specified String.
Syntax:
public Scanner useDelimiter(String pattern)
Parameter: The function accepts a mandatory parameter string pattern which specifies a delimiting pattern.
Return Value: The function returns this scanner.
Below programs illustrate the above function:
Program 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
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)
Recommended Posts:
- Scanner reset() method in Java with Examples
- Scanner hasNextBoolean() method in Java with Examples
- Scanner useRadix() method in Java with Examples
- Scanner useLocale() method in Java with Examples
- Scanner nextBigInteger() method in Java with Examples
- Scanner toString() method in Java with Examples
- Scanner nextByte() method in Java with Examples
- Scanner radix() method in Java with Examples
- Scanner close() method in Java with Examples
- Scanner delimiter() method in Java with Examples
- Scanner hasNextDouble() method in Java with Examples
- Scanner findInLine() method in Java with Examples
- Scanner hasNextFloat() method in Java with Examples
- Scanner hasNextLine() method in Java with Examples
- Scanner nextShort() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



