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

JavaStringSubstring

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));

Solution 2 - Java

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];

Solution 3 - Java

what have you tried? it's very simple:

String s = "/abc/def/ghfj.doc";
s.substring(s.lastIndexOf("/") + 1)

Solution 4 - Java

Another way is to use this.

String path = "/abc/def/ghfj.doc"
String fileName = StringUtils.substringAfterLast(path, "/");

If you pass null to this method it will return null. If there is no match with separator it will return empty string.

Solution 5 - Java

You can use Apache commons:

For substring after last occurrence use this method.

And for substring after first occurrence equivalent method is here.

Solution 6 - Java

This can also get the filename

import java.nio.file.Paths;
import java.nio.file.Path;
Path path = Paths.get("/abc/def/ghfj.doc");
System.out.println(path.getFileName().toString());

Will print ghfj.doc

Solution 7 - Java

With Guava do this:

String id="/abc/def/ghfj.doc";
String valIfSplitIsEmpty="";
return Iterables.getLast(Splitter.on("/").split(id),valIfSplitIsEmpty);

Eventually configure the Splitter and use

Splitter.on("/")
.trimResults()
.omitEmptyStrings()
...

Also take a look into this article on guava Splitter and this article on guava Iterables

Solution 8 - Java

In Kotlin you can use substringAfterLast, specifying a delimiter.

val string = "/abc/def/ghfj.doc"
val result = url.substringAfterLast("/")
println(result)
// It will show ghfj.doc

From the doc:

>Returns a substring after the last occurrence of delimiter. If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.

Solution 9 - Java

I think that would be better if we use directly the split function

String toSplit = "/abc/def/ghfj.doc";

String result[] = toSplit.split("/");

String returnValue = result[result.length - 1]; //equals "ghfj.doc"

Solution 10 - Java

java android

in my case

I want to change from

> ~/propic/........png

anything after /propic/ doesn't matter what before it

> ........png

finally, I found the code in Class StringUtils

this is the code

     public static String substringAfter(final String str, final String separator) {
         if (isEmpty(str)) {
             return str;
         }
         if (separator == null) {
             return "";
         }
         final int pos = str.indexOf(separator);
         if (pos == 0) {
             return str;
         }
         return str.substring(pos + separator.length());
     }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSunnyView Question on Stackoverflow
Solution 1 - JavaSébastien Le CallonnecView Answer on Stackoverflow
Solution 2 - JavaVegerView Answer on Stackoverflow
Solution 3 - Javavishal_aimView Answer on Stackoverflow
Solution 4 - JavaAlex GreenView Answer on Stackoverflow
Solution 5 - JavaMichal PrzysuchaView Answer on Stackoverflow
Solution 6 - JavapushyaView Answer on Stackoverflow
Solution 7 - JavajschnasseView Answer on Stackoverflow
Solution 8 - JavaFilipe BritoView Answer on Stackoverflow
Solution 9 - Javauser6317575View Answer on Stackoverflow
Solution 10 - JavaHassan BadawiView Answer on Stackoverflow