how to split the string in java in Windows? I used Eg.
String directory=\"C:\\home\\public\\folder\";
String [] dir=direct.split(\"\\\");
You need to escape the backslash:
direct.split("\\\\");
Once for a java string and once for the regex.
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.
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
You need to escape it.
String [] dir=direct.split("\\\\");
Edit: or Use Pattern.quote method.
String [] dir=direct.split(Pattern.quote("\\"))