Tiberiu
Tiberiu

Reputation: 1030

Extracting name of an image from an src attribute (url)

I'm trying to scrape images using JSoup and don't understand a piece of code that I stumbled upon.

Part of the code: (src in this case is defined as an absolute url)

private static void getImages(String src) throws IOException {

    String folder = null;

    //Exctract the name of the image from the src attribute
    int indexname = src.lastIndexOf("/");

    if (indexname == src.length()) { // Don't understand this
        src = src.substring(1, indexname);
    }

    indexname = src.lastIndexOf("/");
    String name = src.substring(indexname, src.length());

    // more code
}

I don't understand the if statement. More specifically, when will indexname ever equal the length of src?

Upvotes: 2

Views: 454

Answers (1)

RealSkeptic
RealSkeptic

Reputation: 34618

Don't assume every source you find on the internet is good.

That piece of code has many problems.

  1. Indeed, the only case where the result of String.lastIndexOf is the length of the source string is when the search string is "". So that if block is never executed.
  2. The operation inside that if block (delete the first character of the string) is not really helpful.
  3. It is perfectly legal to add slashes to a URL even after the image name. Try adding '?/' to an image name in a URL.
  4. It is also perfectly legal not to have an image name at all. There could be the name of a script with parameters there, such as "http://example.com/generate-captcha.php?param1=foo&param2=bar" (not a real link, just an example).
  5. You could even have nothing at all after the domain name.

Since there is no law that says that a URL actually has to have a file name after the last slash, or that the file name has to be the name of the actual image, then this code works only part of the time.

Upvotes: 1

Related Questions