Fauzee Official
Fauzee Official

Reputation: 41

Java - Cut a string programmatically

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!

Upvotes: 0

Views: 258

Answers (3)

Jamith NImantha
Jamith NImantha

Reputation: 2227

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

Upvotes: 1

eL_
eL_

Reputation: 177

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

Upvotes: 1

Mushif Ali Nawaz
Mushif Ali Nawaz

Reputation: 3866

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

String value = completeString.substring(completeString.lastIndexOf("/") + 1);

Upvotes: 8

Related Questions