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?
To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character. Copied! We used the String.
Java – Split a String with Specific Character To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
String example = "/abc/def/ghfj.doc"; System.out.println(example.substring(example.lastIndexOf("/") + 1));
A very simple implementation with String.split()
:
String path = "/abc/def/ghfj.doc"; // Split path into segments String segments[] = path.split("/"); // Grab the last segment String document = segments[segments.length - 1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With