0

So I have the following string:

String text = "\t\t\torder #168\n\t\t\tpaid\n\t\t\tview 4 items\n\t\t\tpicked up\n\t\t\tcomplete pickup\n\t\t\t2 stops";

How do I parse this string so that I always get the 2 in front of stops? I have tried the following, but it always returns 2 stops.

String substr = "complete pickup";
String numberOfStops = text.substring(text.indexOf(substr) + substr.length());
numberOfStops = numberOfStops.replaceAll("^\\s+","").replaceAll("\\s+$","");

1 Answer 1

1

The short way:

numberOfStops = numberOfStops.replaceAll("^\\s+","").replaceAll("\\s+$","").replace("stops","");

The flexible way is using Regex, and Pattern and Match classes. Let me know if you need it

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, very quick fix.. I will accept this answer as soon as it lets me.
It doesn't let you ? Weird. Upvote it aswell if you can :) Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.