I’m bit confused with Java doc about Matcher in the definition of start() and end().
Consider the following code:
public static void test()
{
String candidate = "stackoverflow";
Pattern p = Pattern.compile("s");
Matcher m = p.matcher(candidate);
m.find();
int index = m.start();
out.println("Index from Match\t"+index);
int offset = m.end();
out.println("Offset from match\t"+offset);
}
The above will return the following result.
Index from Match 0
Offset from match 1
As I learned every char array or string will start by Index 0 and it’s right in the above expression.
But Offset also returns the same character ‘s’ but why it starts with 1?
No, it doesn’t start with 1 – it starts with 0. The documentation makes it reasonably clear:
(Emphasis mine.)
Basically it’s the end of the match in exclusive form, which is common in Java. It means you can do something like:
Note that your “index” and “offset” should really be regarded as “start” and “end” (hence the method names). The terms “index” and “offset” are effectively synonymous in this context; the important point is that
start()returns the index/offset of the start of the match, andend()returns the index/offset after the end of the match.