This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MatchResult.start() & MatchResult.end() methods

 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All

Here are 2 programs given in JavaBeat Mock exam which are behaving differently.


The above program's output is 5
The next program goes like this...


And the output of this is 2 followed by 8

In the first program we are using scanner.match.start(). In the second one we are using scanner.match.end(). What's the indexing of these two methods? Do they use zero as starting index or 1? Can any one explain me their behavior??? Thanks all!
[ July 08, 2008: Message edited by: Swapna Gouri Kalanidhi ]
 
Ralph Jaus
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna Gouri,

I'll try to explain the results. Let's start with the first one.

1. The first scanner.next() finds "ai ".
2. The second scanner.next() finds "ello".
3. scanner.match().start() returns the starting position of the substring "ello" in the original string "hai hello". This
is 5.
4. scanner.match().end() returns the position after the substring (see API documentation for MatchResult). This is 9.

The second example works in the same fashion. If it still makes problems, just let us know.
[ July 08, 2008: Message edited by: Ralph Jaus ]
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ralph, I got the point. But I still have a doubt, does these methods,viz., match.start()& match.end() use zero indexing or 1 indexing? I mean should we start counting the alphabets from 0 or from 1??
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ralph
Thanks for explaining the behavior of the start() & end() methods. i have tried both the methods practically & I have understood the concept. Thanks a lot for giving a detailed description how both of them work...!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic