Pattern toString() Method in Java with Examples
toString() method of a Pattern class used to return the string representation of this pattern. This return the regular expression from which this pattern was compiled.
Syntax:
public String toString()
Parameters: This method accepts nothing as parameter.
Return value: This method returns a string representation of this pattern.
Below programs illustrate the toString() method:
Program 1:
// Java program to demonstrate // Pattern.toString() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // create a REGEX String String REGEX = "geeks"; // create a Pattern using REGEX Pattern pattern = Pattern.compile(REGEX); // print pattern System.out.println("Pattern:" + pattern); } } |
Pattern:geeks
Program 2:
// Java program to demonstrate // Pattern.toString() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // create a REGEX String String REGEX = "[* & # $ !]+\\S"; // create a Pattern using REGEX Pattern pattern = Pattern.compile(REGEX); // print pattern System.out.println("Pattern:" + pattern); } } |
Pattern:[* & # $ !]+\S
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#toString()
Recommended Posts:
- StringBuilder toString() method in Java with Examples
- Charset toString() method in Java with Examples
- AtomicBoolean toString() method in Java with Examples
- LocalDate toString() method in Java with Examples
- DoubleAdder toString() method in Java with Examples
- Scanner toString() method in Java with Examples
- AtomicIntegerArray toString() method in Java with Examples
- LongAccumulator toString() method in Java with Examples
- DoubleAccumulator toString() method in Java with Examples
- AbstractCollection toString() method in Java with Examples
- StringBuffer toString() method in Java with Examples
- OffsetTime toString() method in Java with examples
- MonthDay toString() Method in Java with Examples
- OptionalLong toString() method in Java with examples
- Calendar toString() 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.



