因為常遇到String中使用 replace 沒有成功的問題,所以特地紀錄一下:
String str = "吾思華、汝思台";
System.out.println("原始的字串 "+str);
//要把"吾"換成"無"
String ch1 = "吾";
String ch2 = "無";
//使用 str.replace(); 這個函數本身會將str中對應到的字元進行替換並且回傳一個替換好的新字串,str本身並沒有被替換掉
//所以如果要用replace,需要用一個新字串來接。
String newStr = str.replace(ch1, ch2);
System.out.println(newStr);
文章標籤
全站熱搜
