impact_Sv
impact_Sv

Reputation: 49

Extracting a string between two Delimiters

I have a String called

ID: 17. Name: Milky way. City: Riverview. Date: 2017-03-21 00:00:00.0

I need to take only the Name which is "Milky way".

This is what i've tried so far

tempString1.substring(tempString1.indexOf("Name:"), tempString1.indexOf("."));

which gives me index out of bounds -2

Upvotes: 2

Views: 271

Answers (1)

Jiri Tousek
Jiri Tousek

Reputation: 12450

You need to use offset when looking for the dot, otherwise it searches from the start of the string and matches the dot in 17..

String label = "Name: ";
int start = tempString1.indexOf(label) + label.length();
String name = tempString1.substring(start, tempString1.indexOf(".", start));

Upvotes: 1

Related Questions