How can we convert String to Map in Java if comma is present in either key or value in `the string? -
lets have,
string value = "{name = abc, address = xyz, abc, school = mno}"
how can convert string map split key value pair of string comma? here when try split comma error saying no value key abc address = xyz acts key value pair.
any generic approach appreciated.
from understand, want create map in entry key address
has xyz, abc
value.
you can use regex this. here example:
string s = "{name = abc, address = xyz, abc, school = mno}"; hashmap<string, string> map = new hashmap<string, string>(); (string element : s.replaceall("[{}]", "").split("(,(?=[^,]+=))")) { string[] entry = element.split("="); map.put(entry[0].trim(), entry[1].trim()); }
what first remove {
, }
character string
, split string
on every ,
followed sequence of characters doesn't contain ,
, ends in =
character.
Comments
Post a Comment