如何用indexOf返回字符串中 "\" 第一次出现的位置?

redfox33 2003-10-17 01:53:41
//返回 "\"第一次出现的位置
String str;
str="abc\def\ghi";

int i
i=str.indexOf("\"); //这样出错,好像是\个符号很特殊,我应该怎么解决?

out.println(i);
...全文
503 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
redfox33 2003-10-17
  • 打赏
  • 举报
回复
up
yaray 2003-10-17
  • 打赏
  • 举报
回复
你可以这样试一下:
String ch = "\\";
System.out.println(ch);//看打出的是什么

int pos = str.indexOf('\\'); // 可能会出错
int pos2= str.indexOf("\\"); // 这个和上面这个应该有一个有效
yaray 2003-10-17
  • 打赏
  • 举报
回复
int pos = str.indexOf('\\'); // 第一个反斜杠是转义符
redfox33 2003-10-17
  • 打赏
  • 举报
回复
to fft123() :
str="e:\download\image\001.gif"

怎么转换成
str="e:/download/image/001.gif"
格式?
fft123 2003-10-17
  • 打赏
  • 举报
回复
用“/”表示路径
然后

String s = "C:/pp/oo/ss.abc";
s = s.replaceAll("/","\\\\");
System.out.println(s);
int i = s.indexOf("\\");
System.out.println(i);
redfox33 2003-10-17
  • 打赏
  • 举报
回复
to tanndy(ben.yu) :
我也想用 /来表示阿
可我从客户端取来的路径就是
e:\download\image\001.gif 这种格式的阿,怎么转换 从 "\" --> "/"?
你有代码吗?
tanndy 2003-10-17
  • 打赏
  • 举报
回复
为什么不采用/表示路径啊?
redfox33 2003-10-17
  • 打赏
  • 举报
回复
to jasher(喜怒哀乐):
String str;
str="e:\download\image\001.gif"; //这是动态从客户端直接获得的路径,我怎么转换成
"e:\\download\\image\\001.gif" ?转换的函数应该怎么写?

int i;
i=str.indexOf("\\");

out.println(i);
luckduck 2003-10-17
  • 打赏
  • 举报
回复
记住:凡是在双引号中的串,不论是一个字符串变量还是一个函数的参数,都必须用"\\"表示"\"
redfox33 2003-10-17
  • 打赏
  • 举报
回复
to masakiyy(冬笋千张):
不行啊,还是出错阿
jasher 2003-10-17
  • 打赏
  • 举报
回复
String str;
str="abc\\def\\ghi";

int i;
i=str.indexOf("\\");

out.println(i);
masakiyy 2003-10-17
  • 打赏
  • 举报
回复
//返回 "\"第一次出现的位置
String str;
str="abc\def\ghi";

int i
i=str.indexOf("\\"); //"\\"转意,输出"\"

out.println(i);
redfox33 2003-10-17
  • 打赏
  • 举报
回复
还是不行啊,
//返回 "\"第一次出现的位置
String str;
str="e:\图片\image\001.gig"; //这是动态得出来的客户本地文件存放的路径

int i
i=str.indexOf("\\"); //这样出错

out.println(i);


gao277 2003-10-17
  • 打赏
  • 举报
回复
\在java中有转义符的意义,用两个\就表示\字符了
gao277 2003-10-17
  • 打赏
  • 举报
回复
i=str.indexOf("\\");

81,092

社区成员

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

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