Pattern toString() Method in Java with Examples Last Updated : 06 Mar, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } Output: Pattern:geeks Program 2: Java // 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); } } Output: Pattern:[* & # $ !]+\S Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#toString() Comment More infoAdvertise with us Next Article Pattern toString() Method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions java-regular-expression Java-Pattern +1 More Practice Tags : Java Similar Reads Pattern quote(String) method in Java with Examples quote(String) method of a Pattern class used to returns a literal pattern String for the specified String passed as parameter to method.This method produces a String equivalent to s that can be used to create a Pattern. Metacharacters or escape sequences in the input sequence will be given no specia 3 min read String toString() Method in java with Examples String toString() is the built-in method of java.lang which return itself a string. So here no actual conversion is performed. Since toString() method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly. Syntax : publ 1 min read Scanner toString() method in Java with Examples The toString() method of java.util.Scanner class returns the string representation of this Scanner. The exact format is unspecified. Syntax: public String toString() Return Value: This function returns the string representation of this scanner. Below program illustrates the above function: Program 1 1 min read Package toString() method in Java with Examples The toString() method of java.lang.Package class is used to get the String representation of this package instance. The method returns the String representation as a String value. Syntax: public String toString() Parameter: This method does not accepts any parameter. Return Value: This method return 1 min read Pattern compile(String) method in Java with Examples The compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.Synta 2 min read Like