我有一個帶空格的字符串,我希望那個空格替換爲"\_"
。例如,這裏是我的代碼如何用java字符串中的「 _」替換空格?
String example = "Bill Gates";
example = example.replaceAll(" ","\\_");
而例子的結果是:「Bill_Gates」而不是「Bill \ _Gates」。當我嘗試做這樣的
String example = "Bill Gates";
example = example.replaceAll(" ","\\\\_");
例子的結果是: 「比爾\\ _門」 不是 「比爾\ _Gates」
相似問題:http://stackoverflow.com/questions/31298982/how-can-i-replace-with-in-java-string –
使用replace,'example.replace(「」,「\\ _」 );' –
@Leo你想要單個反斜槓? –