14 May String replaceAll(String regex, String replacement) method in Java
The replaceAll(String regex, String replacement) method in Java replaces each substring of this string that matches the given regular expression with the given replacement.
Syntax
Let us see the syntax,
1 2 3 |
String replaceAll(String regex, String replacement) |
Parameters
Let us see the parameters,
- regex− regular expression to which this string is to be matched
- replacement− the string that would replace
Example
The following is an example of replaceAll(String regex, String replacement),
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class StudyopediaDemo { public static void main(String args[]) { String message = new String("Studyopedia provides free learning content!"); System.out.println("String: "+message); System.out.print("Result after replacing: " ); System.out.println(message.replaceAll("learning(.*)", "tutorials!")); } } |
Output
The following is the output,
No Comments