0

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.

2
  • Why don't use use JSON? You had better use JSON and use Gson library to parse. {"-LS4kp5hQc6Uodf": {"name": "random", "suggestion": "OK", "id": "kj61sCceDs34Nr1"}} Change like this and use JSON. Commented Nov 28, 2018 at 12:57
  • Please describe in a little detail. Commented Nov 28, 2018 at 12:59

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It was really helpful!

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.