1

so I'm parsing html and I'm trying to create a substring starting at a certain location and stop 941 characters after that. The way the .substring method in Java works is you have to give it a start location and a end location but the end location needs to be a location on the original string after the start.

String html = "This is a test string for example";
html.substring(html.indexOf("test"), 6);

This is an example of how I would like the code to work, it would make a substring starting at test and stop after 7 characters returning "test string". However if I use this code I get a indexOutOfBounds exception because 6 is before test. Working code would be the following

String html = "This is a test string for example";
html.substring(html.indexOf("test"), 22);

Which would return "test string". But I don't know what the last number is going to be since the html is always changing. SO THE QUESTION IS what do I have to do so that I can start a specific location and end a x amount of characters after it? Any help would be much appreciated! Thanks!

2
  • Using + (on the indexOf result) will provide useful information, as will html.length().. anyway, uhm, don't parse HTML by hand. Sep 5, 2014 at 1:43
  • Ya I use that info when I first got it to work, but then the next day the page was different and messed up my values. The html.length() is about 50,000 :/
    – Cris
    Sep 5, 2014 at 1:54

2 Answers 2

2

Since the second parameter is an index, not the length, you need to store the initial position, and add the length to it, like this:

String html = "This is a test string for example";
int pos = html.indexOf("test");
String res = html.substring(pos, pos+11);

Demo.

1
  • That makes perfect sense! I'll try I right now and let you know. Thanks a ton!
    – Cris
    Sep 5, 2014 at 2:02
1

Please refer to the String.substring source.

public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > value.length) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    int subLen = endIndex - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return ((beginIndex == 0) && (endIndex == value.length)) ? this
            : new String(value, beginIndex, subLen);
}

in this source (line 8~9), second parameter bigger than first parameter.

you will need to edit this.

String html = "This is a test string for example";
html.substring(html.indexOf("test"), (6+html.indexOf("test")));

hopefully, that will solve the problem.

1
  • This makes sense as well, and also solves my problem. Thank you very much for taking the time to answer, this is quite clever. I wish I could mark a double correct answer!
    – Cris
    Sep 5, 2014 at 3:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.