Scanner delimiter() method in Java with Examples

The delimiter() method ofjava.util.Scanner class returns the Pattern this Scanner is currently using to match delimiters.

Syntax:

public Pattern delimiter()

Return Value: The function returns the scanner’s delimiting pattern.



Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

Scanner String: 
Geeksforgeeks has Scanner Class Methods

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


Output:

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

Delimiter being used in Scanner: .

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#delimiter()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.