The method replace() replaces all occurrences of a String in another String or all occurrences of a char with another char.
方法replace()将一个字符串在另一个字符串中出现的全部内容替换为另一个字符。
Available Signatures
可用的签名
public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)
Example
示例
@Test
void whenReplaceOldCharNewChar_thenCorrect() {
String s = "wslcoms to basldung";
assertEquals("welcome to baeldung", s.replace('s', 'e'));
}
@Test
void whenReplaceTargetReplacement_thenCorrect() {
String s = "welcome at baeldung, login at your course";
assertEquals("welcome to baeldung, login to your course", s.replace("at", "to"));
}
« Previous