aryo
aryo

Reputation: 94

How can I use .split to split a string AFTER a certain word?

As the title says.

Let's say we have a string called "value"

String value = ("I need to go to the bathroom.");

I want value.split do such a job that it splits the string in half AFTER a certain word.

So that we have an array of strings that has 2 strings inside.

Such as

String[] valuearray = value.split(-whatevercodegoeshere-);

So we get:

valuearray 1 = I need to

valuearray 2 = go to the bathroom.

Is this possible? If not with current functions, is it possible by writing a function specifically designed for this task?

Upvotes: 3

Views: 6265

Answers (1)

Robert Mitchell
Robert Mitchell

Reputation: 892

Try using a lookbeind

value.split("(?<=go) ")

This reads, split where space is preceded by go.

Lookaheads and Lookbehinds don't consume anything, only check if it's there, so go will still be in the String[] returned from split()

Upvotes: 10

Related Questions