public class StringExample
{
public static void main(String[] args)
{
String x = "^^^";
String y = "~";
String s = "abc^^^xyz";
if (s != null && s.indexOf(x) > -1)
s = s.replaceAll(x,y);
System.out.println("s :" + s);
}
}
它沒有給出正確的結果。想要用「〜」替換「^^^」說abc ^^^ xyz應該被替換爲abc〜xyz
輸入
ABC ^^^ XYZ
實際輸出
〜A〜B〜C〜^〜^〜^〜X〜ÿ 〜z
期望輸出
ABC〜XYZ
的replaceAll()接受一個正則表達式作爲參數..你需要躲避'^單曲 – TheLostMind
什麼,我不明白的是,爲什麼你做了''aaa ^^^ bbb「.replaceAll(」^^^「,」〜「)''你有''a〜b〜c〜^〜^〜^〜x〜y〜z',而不是'〜aaa ^^^ bbb'? – Kent