How to use .substring method to start at an index and grab x number or characters after it?

前端 未结 2 397
轻奢々
轻奢々 2021-01-25 04:15

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

2条回答
  •  Happy的楠姐
    2021-01-25 04:49

    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.

提交回复
热议问题