Home > OS >  Splitting a String at last occurrence of slash
Splitting a String at last occurrence of slash

Time:12-27

In java I can split at the last occurrence of a character (dot in this case) like this: String[] parts2 = path.split("\\.(?=[^.]*$)"); String Folderpath = parts2[0]; But when I try to split it at the last occurrence of a slash, like this: String[] parts2 = path.split("\\/(?=[^.]*$)"); String Folderpath = parts2[0]; it gives me following Warning: Redundant character escape '\/' in RegExp

Any help would be appreciated!

CodePudding user response:

You're using the wrong tool for the job.

String folderPath = path.substring(0, path.lastIndexOf('/'));

But your actual problem is that '/' is not special in Java regular expression syntax and therefore does not need to be escaped with '\'. That's what the warning message is telling you. Delete the backslash that precedes '/'.

  • Related