简体   繁体   English

如何拆分 java 中的字符串

[英]how to split the string in java

how to split the string in java in Windows?如何在 Windows 中拆分 java 中的字符串? I used Eg.我用了例如。

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

I want to know how to split the string in eg.我想知道如何将字符串拆分为例如。 In java, if I use "split("\")" , there is syntax error.在 java 中,如果我使用"split("\")" ,则会出现语法错误。

thanks谢谢

split() function in Java accepts regular expressions. split() Java 中的 function 接受正则表达式。 So, what you exactly need to do is to escape the backslash character twice:因此,您真正需要做的是两次转义反斜杠字符:

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

One for Java, and one for regular expressions.一种用于 Java,一种用于正则表达式。

The syntax error is caused because the sing backslash is used as escape character in Java.语法错误是因为在 Java 中使用 sing 反斜杠作为转义字符

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.但是您应该使用java.io.File.separator作为路径中的拆分字符。

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

thx to John谢谢约翰

You need to escape the backslash:您需要转义反斜杠:

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

Once for a java string and once for the regex.一次用于 java 字符串,一次用于正则表达式。

You need to escape it.你需要逃避它。

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

Edit : or Use Pattern.quote method.编辑:或使用Pattern.quote方法。

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

Please, don't split using file separators.请不要使用文件分隔符进行拆分。

It's highly recommended that you get the file directory and iterate over and over the parents to get the paths.强烈建议您获取文件目录并遍历父目录以获取路径。 It will work everytime regardless of the operating system you are working with.无论您使用的是什么操作系统,它都可以随时工作。

Try this:尝试这个:

String yourDir = "C:\\home\\public\\folder";
File f = new File(yourDir); 
System.out.println(f.getAbsolutePath());
while ((f = f.getParentFile()) != null) {
    System.out.println(f.getAbsolutePath());
}
final String dir = System.getProperty("user.dir");
String[] array = dir.split("[\\\\/]",-1) ;
 String arrval="";

   for (int i=0 ;i<array.length;i++)
      {
        arrval=arrval+array[i];

      }
   System.out.println(arrval);

I guess u can use the StringTokenizer library我想你可以使用 StringTokenizer 库

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.这在语法上可能并不完全正确,但希望这会有所帮助。

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这将给出结果为abc

It's because of the backslash.这是因为反斜杠。 A backslash is used to escape characters.反斜杠用于转义字符。 Use利用

split("\\")

to split by a backslash.用反斜杠分割。

split("\\") A backlash is used to escape. split("\\")反冲用于转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM