I have a string
receipt from a scale, from which I need to subtract the weight values it contains. I used the function split()
to split the string
into three parts. My question is how can I subtract only the weight values, removing everything else from the string
?
My code is the following
String str = "ST A011 0.00kg A012 0.00kg A013 0.00kg";
String[] parts = str.split("kg");
String part1=parts[0]; // ST A011 0.00
String part2=parts[1]; // A012 0.00
String part3=parts[2]; // A013 0.00
What I'm looking for would be:
String part1=parts[0]=0.00
String part2=parts[1]=0.00
String part3=parts[2]=0.00
Keeping your code as it is, you can do:
With the
substring
we are discarding everything that is before the0.00
, the+ 4
because eachA011, A012, A013
has a size of 4, for which we must add it to the index of the first coincidence ofA011, A012, A013
respectively and thetrim
to eliminate the white spaces that remain.