Java: Getting a substring from a string starting after a particular character

Sunny picture Sunny · Jan 14, 2013 · Viewed 421.1k times · Source

I have a string:

/abc/def/ghfj.doc

I would like to extract ghfj.doc from this, i.e. the substring after the last /, or first / from right.

Could someone please provide some help?

Answer

Sébastien Le Callonnec picture Sébastien Le Callonnec · Jan 14, 2013
String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));