String fname="C:\textfiles\db\query\query.txt";
this is the string i need to split it.
I tried with this
String [] items=fname.split("\");
But not working.
String [] items=fname.split("\\"); also not working...
how to split this string...
The str. split method will split the string on each occurrence of a backslash and will return a list containing the results. Copied!
Java split() function is used to splitting the string into the string array based on the regular expression or the given delimiter. The resultant object is an array contains the split strings.
Java For TestersA character preceded by a backslash (\) is an escape sequence and has a special meaning to the compiler. The following table shows the Java escape sequences. Inserts a tab in the text at this point. Inserts a backspace in the text at this point.
First of all you can not have a string as you posted in question
String fname="C:\textfiles\db\query\query.txt";
this should be replaced by
String fname="C:\\textfiles\\db\\query\\query.txt";
as backslash("\") needs an escape as well.
Finally you need to do something like this to split them:
String fname="C:\\textfiles\\db\\query\\query.txt"; String[] items= fname.split("\\\\"); System.out.println(Arrays.toString(items));
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With