How to split a java string at backslash

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.

Leave a Comment