indexOf()

I'm trying to break apart a string using indexOf() and the commas as reference points. it's easy to find the first comma and it's value but when i try to get the second comma's value it doesn't work.

Ex) String = 5, 8, 10
position1 = String.indexOf(",");
position2 = String.indexOf(",", position1); *shouldn't this search for commas afeter the initial comma? Instead pos1 and pos2 values are identical.*

How can i use indexOf() to search for the same character? It always returns the value of the first time it appears and thats it!

Comments

  • : I'm trying to break apart a string using indexOf() and the commas as reference points. it's easy to find the first comma and it's value but when i try to get the second comma's value it doesn't work.
    :
    : Ex) String = 5, 8, 10
    : position1 = String.indexOf(",");
    : position2 = String.indexOf(",", position1); *shouldn't this search for commas afeter the initial comma? Instead pos1 and pos2 values are identical.*
    :
    : How can i use indexOf() to search for the same character? It always returns the value of the first time it appears and thats it!
    :
    :
    IndexOf() searches from the position, and including it. So the second statement finds the first comma from (including) position1. The solution is to search after position1, i.e. position1+1.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion