62,584
社区成员




public class Test {
public static String formatPath(String input) {
if (input.indexOf("//")!=-1){
return formatPath(input.replace("//","/"));
}
return input;
}
public static void main(String args[]) {
String path = "c:\\\\\\\\\\d\\\\c";
String opath = formatPath(path.replace("\\","/"));
System.out.println(opath);
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TempTest {
private static Pattern pattern;
private static Matcher matcher;
public static String formatPath(String input) {
pattern = Pattern.compile("\\\\{2,}");
matcher = pattern.matcher(input);
String out = matcher.replaceAll("\\\\");
return out;
}
public static void main(String args[]) {
String path = "c:\\\\\\\\\\d\\\\c";
String opath = formatPath(path);
System.out.println(opath);
}
}
这个只对windows系统有效,如果是linux的或者其他操作系统,路径分割符会是"/",因此希望高手指点一个好的方法。