replace method: support the replacement characters and strings.
public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement)
replaceAll Method: replacement string based on regular expression.
public String replaceAll(String regex, String replacement)
Test code:
String str = "Hello Java. Java is a language."; System.out.println(str.replace("Java.", "c++"));//打印 Hello c++ Java is a language. System.out.println(str.replaceAll("Java.", "c++"));//打印 Hello c++ c++is a language.
Print Results:
Hello c++ Java is a language. Hello c++ c++is a language.
ps: once thought replace () is the first match of the character or string replacement, replaceAll () is a character string or string replacement target all match, from the results of this perception is wrong.
- Java self-study guide
- Java interview questions summary PC client browser [Click here]
- Java mapping knowledge
- Java applets interview questions summary view, sweep the two-dimensional code