如何用反斜槓替換Java字符串中的空格字符?用反斜槓替換Java字符串中的空格
我嘗試這樣做:
String text = " 1 2 3 4 5 6 7 8 9 10";
text = text.replace(" ","\\");
System.out.println(text2);
但在Eclipse中調試變量的結果一看:
text 1 2 3 4 5 6 7 8 9 10
text2 \\1\\2\\3\\4\\5\\6\\7\\8\\9\\10
,但在我的日誌結果是不同的:
07-18 14:56:31.049: I/System.out(9177): \1\2\3\4\5\6\7\8\9\10
我想用smb命令來允許目錄文件名中的空格。這個命令在我的代碼中手動設置時有效。結果應該是這樣的:
source : smb://192.168.0.254/Disque dur/
text = text.replace(" ","\040");
result : smb://192.168.0.254/Disque\040dur/
但是,當我使用replace()方法有雙反斜線...
result : smb://192.168.0.254/Disque\\040dur/
07-18 15:15:10.439: E/Home(9538): jcifs.smb.SmbException: The network name cannot be found.
感謝您的幫助
刪除空間所以你要替換'\'所有的空格,'\\'或'\ 040'? –