Java 字符串 replace() 方法
示例
返回一个新的字符串,其中所有 "l" 字符都被替换为 "p" 字符
String myStr = "Hello";
System.out.println(myStr.replace('l', 'p'));
定义和用法
The replace()
method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
语法
public String replace(char searchChar, char newChar)
参数值
参数 | 描述 |
---|---|
searchChar | A char , representing the character that will be replaced by the new character |
newChar | A char , representing the character to replace the searchChar with |
技术细节
返回值 | A new String , where the specified character has been replaced by the new character(s) |
---|
❮ 字符串方法