LegendaryLaggy
LegendaryLaggy

Reputation: 455

Using indexOf in Java

I am trying to use indexOf(" "); on this string

"This i$ an aardvaAr yeEeeAs onDo qwerty XYZ9";

this is my code

        space = word.indexOf(" ");

        tempWord = word.substring(0, space);

Now I get what I want which is This string but now how do I get the next space which has this after it:i$, and the next one unitl the end of the string?

*EDIT

Please no arrays for this question

Upvotes: 2

Views: 6474

Answers (2)

fazhool
fazhool

Reputation: 121

if you don't want to split the string (and create an String array) you can read the rest of the string after hitting the first space (btw. this seems like a schoolwork on recursive functions):

int space = test.indexOf(" ");
String before = test.substring(0, space);
String after = test.substring(space + 1);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500675

Use the overload of indexOf which takes the starting index too:

int nextSpace = word.indexOf(" ", space + 1);

(Although there may very well be a better approach to your bigger problem.)

Upvotes: 6

Related Questions