2 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
public String replace(char oldChar,
char newChar)返回一個(gè)新的字符串,它是通過用 newChar 替換此字符串中出現(xiàn)的所有 oldChar 得到的。
如果 oldChar 在此 String 對(duì)象表示的字符序列中沒有出現(xiàn),則返回對(duì)此 String 對(duì)象的引用。否則,創(chuàng)建一個(gè)新的 String 對(duì)象,它所表示的字符序列除了所有的 oldChar 都被替換為 newChar 之外,與此 String 對(duì)象表示的字符序列相同。
示例:
"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoise".replace('p', 't')
returns "starring with a turtle tortoise"
"JonL".replace('q', 'x') returns "JonL" (no change)
參數(shù):
oldChar - 原字符。
newChar - 新字符。
返回:
一個(gè)從此字符串派生的字符串,它將此字符串中的所有 oldChar 替代為 newChar。

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
class metho {
public metho() {
}
public static void main(String[] args) {
String firstString = "This is";
String secondString = "Thisis";
if (firstString.equals(secondString)) {
System.out.println("相等");}
else{
System.out.println("不相等");
}
System.out.println(firstString.concat(" hisfather"));
System.out.println(firstString.replace(firstString.lastIndexOf('i'),'z' ));
}
}
添加回答
舉報(bào)