-1

i have this string:

http://www.website.com/path/photo.jpg?resize=600%2C400

and i want to remove the part after the file type. With other words i want to get

http://www.website.com/path/photo.jpg

the file type that can exist are .jpg, .png, .gif

any idea how can i implement this?

2 Answers2

0

One way to achieve this would be to find the first locaiton of he character '?' in your string and then truncate your string till that location.

String modifiedUrl = "";
if(url.contains("?"))
{
   modifiedUrl = url.substring(0, url.indexOf("?"));
}
Kakarot
  • 4,252
  • 2
  • 16
  • 18
0

A more URL-specific solution would be to use the logic in the URL class, which will be smarter than any regex your can create in the same amount of time.

URL fullUrl = new URL("http://www.website.com/path/photo.jpg?resize=600%2C400");
System.out.println(fullUrl.getProtocol() + "://" + fullUrl.getHost()
                   + ":" + fullUrl.getPort() + fullUrl.getPath());

You'll want exception handling and null-handling in there to handle cases where bits of the URL aren't provided, like /localDir/localFile or ftp://yourServer.yourDomain/file.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78