Zubayr
Zubayr

Reputation: 456

Get Specific substrings from strings in JAVA

I have strings of the format "{-LS4kp5hQc6Uodf={name=random, suggestion=OK, id=kj61sCceDs34Nr1}}". Here I want to take the name substring (here "random") and suggestion substring (here "OK") to 2 different string. How to do that?

P.S.- the format of all strings are similar.

Upvotes: 0

Views: 45

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 51821

You can use regular expressions if you don't want to use json.

String data = "{-LS4kp5hQc6Uodf={name=random, suggestion=OK, id=kj61sCceDs34Nr1}}";
Pattern pattern = Pattern.compile("name=(.*),.*suggestion=(.*),");
Matcher matcher = pattern.matcher(data);
if (matcher.find()) {
    System.out.println(matcher.group(1)); //name value
    System.out.println(matcher.group(2));//suggestion value
}

Upvotes: 2

Related Questions