• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding string occurrences in a string  RSS feed

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I am looking for a specific occurrence of a string in a larger string.
Now im aware that the method Sting.indexOf(occurrence) will return the integer position of the first occurrence of the string im looking for, but i know that the string im looking for appears more than once in the larger string and i would like to find the integer position of each occurrence of the string im looking for. Would anybody know of any code that code do this for me.

Best
Mark
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two-argument versions of indexOf that take a start index inside the String. So the way you use this is you write a loop with an index that starts at zero, and you call indexOf(). While the return value is not -1, you use the return value + 1 as the new index, and you "walk" along the String.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest,
Think i have an idea what you mean, ill have a go at it and get back to you thanks.

Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!