Note that the first split on the pipe (|
) is going to give you a list of strings like
['11, -22', '32.45, 34', '32, 41', '32, 12.12']
So to process this further, you'll either need to split()
again, or just use replace()
with a simple regex.
ex.
// Doesn't hurt to do this outside of the loop, but it also doesn't help
//List<String> parts = testStr.split('\\|');
List<String> processed = new List<String>();
for(String part :testStr.split('\\|')){
system.debug(part); // should print things like '32.45, 34'
// Using some simple regex with capturing groups to get the a/b part of the format
// The $1 and $2 get replaced with the first and second capturing groups
// (the stuff contained inside the `()`) respectively
// [0-9.-]+ covers integer strings, decimal strings, and negative numbers
processed.add(part.replace('([0-9\\-.]+), ([0-9\\-.]+)', '"a":$1,"b":$2));
}
The result of that should be ['"a":11,"b":-22', "a":32.45,"b":34', '"a":32,"b":41','"a":32,"b":12.12']
. The square braces just indicate that it's part of a list.
From there, it should be a simple String.join()
to get the final output