how to split the string in java

后端 未结 10 1695
梦如初夏
梦如初夏 2020-12-11 10:44

how to split the string in java in Windows? I used Eg.

String directory=\"C:\\home\\public\\folder\";
String [] dir=direct.split(\"\\\");
         


        
10条回答
  •  醉酒成梦
    2020-12-11 11:16

    The syntax error is caused because the sing backslash is used as escape character in Java.

    In the Regex '\' is also a escape character that why you need escape from it either.

    As the final result should look like this "\\\\".

    But You should use the java.io.File.separator as the split character in a path.

    String[] dirs = dircect.split(Pattern.quote(File.separator));
    

    thx to John

提交回复
热议问题