一个简单的replaceAll方法为何出PatternSyntaxException 代码如下File f=new File("C:/birds_r/basefile"); System.out.println(f.toString().replaceAll("\\", "/")); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主看看Java的API String replaceAll(String regex, String replacement) 使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。 所以楼主的代码应该为:File f=new File("C:/birds_r/basefile"); System.out.println(f.toString().replaceAll("/", "\\")); 呵呵。楼主多试验两次嘛不然就看下API啊 把"/"替换成"\":File f=new File("C:/birds_r/basefile");System.out.println(f.toString().replaceAll("/", "\\"));把"\"替换成"/":File f=new File("C:/birds_r/basefile");System.out.println(f.toString().replaceAll("\\\\", "/")); // 1.首先如1楼所说,LZ可能题目意思理解错了,至少应该这样:File ff = new File("C:/birds_r/basefile");System.out.println(ff.toString().replaceAll("/", "\\"));// 2.如果真的是想将"\\"替换成"/",那么应该这样:File ff = new File("C:\\birds_r\\basefile");System.out.println(ff.toString().replaceAll("\\\\", "/"));// 为什么要用"\\\\"呢,因为"\"在正则表达式是是特殊符号,所以用两个"\"表示"\",在正则表达式中就需要两个"\".// 但java中使用两个"\"表示一个"\",所有就要用4个"\"表示2个"\"。 敌机路线设计的问题 我见过的最好的视频教程网站. 请问windowXP系统中java可不可以进行数据库操作? Collection下的一些东西 怎么获得抽象类的对象 请问 string.class 是什么意思,为什么我查jdk上没有说呢?谢谢! 有谁能告诉我,这个程序哪个地方错了!!(新) 你们公司用什么开发工具 ByteArrayInputStream bais的中文问题... 谁有SqlServer的JDBC驱动?谢谢!定给分!! 求助!java将字符串加密为oracle数据库raw类型! java编程问题
String replaceAll(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。
所以楼主的代码应该为:File f=new File("C:/birds_r/basefile");
System.out.println(f.toString().replaceAll("/", "\\"));
不然就看下API啊
System.out.println(f.toString().replaceAll("/", "\\"));把"\"替换成"/":File f=new File("C:/birds_r/basefile");
System.out.println(f.toString().replaceAll("\\\\", "/"));
// 1.首先如1楼所说,LZ可能题目意思理解错了,至少应该这样:
File ff = new File("C:/birds_r/basefile");
System.out.println(ff.toString().replaceAll("/", "\\"));// 2.如果真的是想将"\\"替换成"/",那么应该这样:
File ff = new File("C:\\birds_r\\basefile");
System.out.println(ff.toString().replaceAll("\\\\", "/"));
// 为什么要用"\\\\"呢,因为"\"在正则表达式是是特殊符号,所以用两个"\"表示"\",在正则表达式中就需要两个"\".
// 但java中使用两个"\"表示一个"\",所有就要用4个"\"表示2个"\"。