how to split the string in java

后端 未结 10 1685
梦如初夏
梦如初夏 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:30

    You need to escape the backslash:

    direct.split("\\\\");
    

    Once for a java string and once for the regex.

    0 讨论(0)
  • 2020-12-11 11:31

    I guess u can use the StringTokenizer library

    String directory="C:\home\public\folder"; 
    String [] dir=direct.split("\");
    StringTokenizer token = new StringTokenizer(directory, '\');
    while(token.hasTokens()
    {
        String s = token.next();
    }
    

    This may not be completely correct syntactically but Hopefully this will help.

    0 讨论(0)
  • 2020-12-11 11:31
    String[] a1 = "abc bcd"
    String[] seperate = a1.split(" ");
    String finalValue = seperate[0];
    System.out.pritln("Final string is :" + finalValue);
    

    This will give the result as abc

    0 讨论(0)
  • 2020-12-11 11:36

    You need to escape it.

    String [] dir=direct.split("\\\\");
    

    Edit: or Use Pattern.quote method.

     String [] dir=direct.split(Pattern.quote("\\"))
    
    0 讨论(0)
提交回复
热议问题