如何将 \ 转换成 /?

redfox33 2003-10-17 04:30:03
怎么将str="d:\photo\map\chinese.gif"转换为
str="d:/photo/map/chinese.gif"
...全文
72 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
caodongxiao 2003-12-24
  • 打赏
  • 举报
回复
你的帖子太好结了
str = str.replaceAll("\\\\","/");

比如“d:\photo\map\chinese.gif”的字符串,其实在java里是象“d:\\photo\\map\\chinese.gif”这样存的,所以只能用"\\\\",这才是替换的方法!
acefr 2003-10-17
  • 打赏
  • 举报
回复

http://chinaunix.net/jh/26/69735.html
redfox33 2003-10-17
  • 打赏
  • 举报
回复
我在做文件上传,用户从客户端选中图片后,上传到服务器upload文件夹下,并将文件名取出出入数据库,我现在就是想知道如何从客户端的路径中获得文件名!
现在我已经可以得到客户端文件的存放路径 str="d:\photo\map\chinese.gif";
但我无法在服务器端从"d:\photo\map\chinese.gif"中取出 chinese.gif,因为这个路径不是
"d:\\photo\\map\\chinese.gif"这样的格式。
liyujie2000 2003-10-17
  • 打赏
  • 举报
回复
请相信拥有两颗红星的同志
soloxiao 2003-10-17
  • 打赏
  • 举报
回复
String s1="d:\\photo\\map\\chinese.gif";
s1=s1.replace((char)92,(char)47);

replaceAll有版本限制,好像是要1.4
redfox33 2003-10-17
  • 打赏
  • 举报
回复
这是我从客户端获取的用户文件路径阿
str="d:\photo\map\chinese.gif";
acefr 2003-10-17
  • 打赏
  • 举报
回复
jkit(郁闷之堕落的程序员) 和 tanndy(ben.yu) 说的是对的

str="d:\photo\map\chinese.gif";//这句是不能通过的因为\是特殊字符,需要转义一下
改成这样:
String str="d:\\photo\\map\\chinese.gif";
String stemp=str.replaceAll("\\\\","/");
就可以通过了
redfox33 2003-10-17
  • 打赏
  • 举报
回复
to acefr():
\\\\也是错的阿,你可以试一下
acefr 2003-10-17
  • 打赏
  • 举报
回复
String str="d:\photo\map\chinese.gif";
String stemp=str.replaceAll("\\\\","/");
^-------------------------------改一下,刚才打成Strint了
jkit 2003-10-17
  • 打赏
  • 举报
回复
应该是你的str="d:\photo\map\chinese.gif"这句话编不过吧。
改为str="d:\\photo\\map\\chinese.gif"
tanndy 2003-10-17
  • 打赏
  • 举报
回复
键盘有问题啊,老是写错~:)

应该是:
str = str.replace('\\','/');

不过你那样的字符串在JAVA里不允许,应该
str="d:\\photo\\map\\chinese.gif";
acefr 2003-10-17
  • 打赏
  • 举报
回复
String str="d:\photo\map\chinese.gif";
Strint stemp=str.replaceAll("\\\\","/");
aku0708 2003-10-17
  • 打赏
  • 举报
回复
str = str.replace("\\","/");
redfox33 2003-10-17
  • 打赏
  • 举报
回复
不行啊,出错阿,不信你自己试一试
tanndy 2003-10-17
  • 打赏
  • 举报
回复
~:)写错了
应该是:
str = str.replace("\\","/");
tanndy 2003-10-17
  • 打赏
  • 举报
回复
str = str.replace("\\","/";)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧