如何用反斜杠替换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'? –