Ethan Allen
Ethan Allen

Reputation: 14835

How do I take the end off of a String in Java/Android?

I have a string that looks like this:

http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg

I just want the end of the string past the / (%2F) so I get the following:

024543634737.jpg

Is there a RegEx or something that I can use with Java? Can someone post some quick code?

Upvotes: 0

Views: 119

Answers (5)

Yogendra Singh
Yogendra Singh

Reputation: 34367

if(myString.indexOf("/")>=0){  
    myString = myString.substring(myString.lastIndexOf("/")+1, myString.length());
}

Upvotes: 0

Liu guanghua
Liu guanghua

Reputation: 991

String splitString = "/"; // you can change it to %2F
String s = "http://www.myurl.com/barcodes/images/024543634737.jpg";
int index = s.lastIndexOf(splitString);
String result= null;
if(index > -1){
    result = s.substring(index+splitString.length());
}

Upvotes: 4

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

String[] array= "http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg".split("%2F");

String own = array[array.length-1];

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

Try the following (using String.lastIndexOf and String.substring methods) -

String input = "http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg";
System.out.println(input.substring(input.lastIndexOf("%2F") + 3));

Output:

024543634737.jpg

Upvotes: 1

Lucifer
Lucifer

Reputation: 29652

you can use lastIndexOf() method of String class. lastIndexOf() Searches for the last occurrence of a character or substring. see like this,

String str = "http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg";

int i = str.lastIndexOf("%");

String result = str.substring ( i );

Upvotes: 0

Related Questions