Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a java string at backslash

Tags:

java

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...

like image 386
Subbarao Gaddam Avatar asked May 20 '14 05:05

Subbarao Gaddam


People also ask

How do you split a string in backslash?

The str. split method will split the string on each occurrence of a backslash and will return a list containing the results. Copied!

What does split \\ do in Java?

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.

How do you handle a backslash in Java?

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.


1 Answers

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.

like image 76
Sanjeev Avatar answered Sep 27 '22 01:09

Sanjeev