Three JS replace the specified character or string function mainly to give a different example to explain how to replace the string ah, text replacement (first occurrence of characters) text replacement (all) text replacement (substitution character) Three kinds of situation oh.
Text substitution (substitution character)
Pattern Pattern.compile ("regular expression");
Matcher Matcher = Pattern.matcher ("Regular expression Hello world, regular expression Hello World");
StringBuffer sbr = new StringBuffer ();
while (Matcher.find ()) {
Matcher.appendreplacement (SBR, "Java");
}
Matcher.appendtail (SBR);
System.out.println (Sbr.tostring ());
Text substitution (first occurrence of characters)
Pattern Pattern.compile ("regular expression");
Matcher Matcher = Pattern.matcher ("Regular expression Hello world, regular expression Hello World");
Replace the first data that matches the regular
System.out.println (Matcher.replacefirst ("Java"));
Text substitution (All)
Pattern Pattern.compile ("regular expression");
Matcher Matcher = Pattern.matcher ("Regular expression Hello world, regular expression Hello World");
Replace the first data that matches the regular
System.out.println (Matcher.replaceall ("Java"));