To replace *' with '^' using Java Regular Expressions, we need to use the replaceAll() method. The replaceAll() method returns a String replacing all the character sequence matching the regular expression and String after replacement.
Declaration − The java.lang.String.replaceAll() method is declared as follows −
public String replaceAll(String regex, String replaced)
Let us see a program to replace '*' with '^' using Java Regular Expressions −
public class Example { public static void main( String args[] ) { String str = new String("H*e*l*l*o"); System.out.println( "Initial String : "+ str); // replacing '*' with '^' str = str.replaceAll( "\\*", "^" ); System.out.println( "The String after substitution : "+str ); } }
Initial String : H*e*l*l*o The String after substitution : H^e^l^l^o