I have below String which is in the format of key1=value1, key2=value2
which I need to load it in a map (Map<String, String>)
as key=value
so I need to split on comma ,
and then load cossn
as key and 0
its value.
String payload = "cossn=0, itwrqm=200006033213";
Map<String, String> holder =
Splitter.on(",").trimResults().withKeyValueSeparator("=").split(payload);
I am using Splitter here to do the job for me but for some cases it is failing. For some of my strings, value has some string with equal sign. So for below string it was failing for me:
String payload = "cossn=0, abc=hello/=world";
How can I make it work for above case? For above case key
will be abc
and value should be hello/=world
. Is this possible to do?
The method split(CharSequence) in the type Splitter.MapSplitter is not applicable for the arguments (String, int)
. – ReedbuckString.split
, which you aren't using (but which Tomer's answer below uses).MapSplitter.split
doesn't have any overload that takes a number of splits, but you can use thelimit()
API onSplitter
to get the same effect. – Orientation