Parse a string with delimiters and load it in a map?

前端 未结 2 1582
暖寄归人
暖寄归人 2021-01-25 23:13

I have below String which is in the format of key1=value1, key2=value2 which I need to load it in a map (Map) as key=value<

2条回答
  •  温柔的废话
    2021-01-26 00:14

    As you said your keys only contain alphanumerics, the following would probably be a good heuristic for splitting:

    payload.split("\\s*,\\s*(?=[a-zA-Z0-9_]+\\s*=|$)");
    

    Which will split on probably whitespace framed commas that are followed by the end of the string or an alphanumeric key, optional whitespace and an equals sign.

提交回复
热议问题