\
is an escape character. If you are using "\"
, its like escaping "
character. Java understands it as " \" (quote missing)
. Hence the error to close the quote appears.
To resolve it you need to escape the \
character.
String word = str.substring(str.lastIndexOf("\\"));