So I am not really familiar with the java String methods and I am not sure if there’s an easy way to do this. I have a String which contains couple of values delimited with “:” and each couple is separated from the others by another delimiter “,”. For example:
AA:BB,CC:DD,XX:YY,EE:FF
The mapping is “from” and “to” value. Based on a condition, I get the “from” value, let’s take for example “XX”, and I want to retrieve the “to” value in this case “YY”.
I am not really sure how to do it. Any help will be appreciated.
Thanks 🙂
You can convert the string into a map:
Basically, you split the string twice. First, you break it into the individual key-value pairs by splitting it with
split(","), then loop through all of these pairs. We split each pair once again, this time by the colon, then store the value in a key-value map which maps strings keys to string values.You can then read a value from the map like so:
For example, using your example data: