0

I have a string (URL) like this:

"https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp"

I need to extract the last part only i.e. 02_Cuppy_lol.webp.

How can I do that?
Thanks!

3
  • 4
    Split on / and retrieve the last element. Commented Dec 7, 2018 at 6:19
  • 6
    You should first try to do it by yourself and post your progress here, this way you encourage people to help you too. Commented Dec 7, 2018 at 6:22
  • 2
    I see that you're a new contributor, welcome to the site. Please read the help docs. Start here (stackoverflow.com/help/on-topic). You should always do some research before you ask and you should also post the things you tried Commented Dec 7, 2018 at 6:26

3 Answers 3

8

You can use substring() and lastIndexOf() here:

String value = completeString.substring(completeString.lastIndexOf("/") + 1);
Sign up to request clarification or add additional context in comments.

2 Comments

why i use "+ 1" at last?
@FauzeeOfficial Because lastIndexOf() returns the index of last / but we don't want it to be included in our substring. That's why we skipped that index. I hope you got it?
1

You can split this text/url and get last part, for example:

String url = "https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] splittedUrl = url.split("/");
String lastPart = splittedUrl[splittedUrl.length()-1)];

Comments

1

you can use the method split().follow this example

public class Demo {
    public static void main(String args[]){
        String str ="https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
        String[] temp=str.split("/");
        int lastIndex =temp.length-1;
        String lastPart = temp[lastIndex];
        System.out.println(lastPart);
    }
}

Output-:

02_Cuppy_lol.webp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.