Yes, the code you provide manipulates the string by extracting the comma position and splitting the string. You can check any the docs for String indexOf to find a full explanation of this function.
sonar_distance= val.substring(index1+1, val.length());
is actually taking the value after the comma to the end of the string. Sonar distance will contain the last two values (out of three). If you want to use this algorithm, one approach is to detect the comma and split this value into sonar distance and ir distance.
I encourage you to use more flexible approaches as it is easier to read, easier to maintain, easier to update and in short, makes your life easier. If you want to do character manipulation, then I find it is easier to take pen and paper and figure out the algorithm you need and then implement the code.
String angle, sonarDista, irDista;
String[] rawData = val.split(',');
if(rawData.size()==3){ //Expecting three values
angle = rawData[0];
sonarDista = rawData[1];
irDista = rawData[2];
}
Kf