String的replace()方法
replace()方法有两种。
-
通过用
newChar
字符替换字符串中出现的所有oldChar
字符,并返回替换后的新字符串。public String replace(char oldChar, char newChar)
举例:字符串格式的日期”/“和”-“转换:
String newStr = "2022/05/06".replace("/","-");
-
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
String replace(CharSequence target, CharSequence replacement)
举例:把字符串
s
中的每个空格替换成"%20":return s.replace(" ","%20");