Hello,
I am trying to extract the first group and the second group of up to 3 numbers for the following type of string:
AZ123.4 EL134.5 or AZ90.3 EL 2.3 or similar - see below:
The string will always start with AZ and have up to 3 number, a decimal point and single number following the decimal point. It is the same with the number that follows the EL characters.
All I want is the the Integer of the number following the AZ characters and load this into a variable called az. Once I have got this, I then want to extract the number following EL and again, load this into a variable called el, I only need the integer of the number. The number in both cases can be from 1 to 3 digits long with 1 further number after the decimal point.
I have tried looking for the "Z" character and then extracting the 1 to 3 numbers between it and the decimal point and the same with the "L" character and then extracting the 1 to 3 numbers that follow it.
Is there a straight forward way of doing this. Any help would be really appreciated. Bill
There's a standard C library function called strtok() that is designed to do this type of parsing. That said, in this world, the String class is not always a good idea, with char arrays being preferred. Your data seem well-formed, so writing your own routines wouldn't be hard, plus you'd learn something along the way...always a good thing.
Is there a straight forward way of doing this. Any help would be really appreciated. Bill
If you have control over the way the data is sent, the below format would make the receiving and parsing of the incoming data fairly easy. In the data packet have number first, then the number identifier, followed by an end of packet delimiter (the comma below). Your current format can be parsed if the space between the data strings can be used as a delimiter, but might take a couple extra lines of code.
Found I had some code that might be of use. I modified it to look for a space instead of a comma and the AZ and EL identifiers. You can test by sending your strings followed by a space using the arduino serial monitor.
//zoomkat 3-5-12 simple delimited ',' string
//from serial port input (via serial monitor)
//and print result out serial port
String readString, data;
int CD, CM, CT, CS, BR;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like CD01,CM01,CT01,CS03,BR255,
//AZ123.4 EL134.5 AZ90.3 EL2.3
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ' ') {
Serial.println(readString); //prints string to serial port out
if(readString.indexOf("AZ") >=0) {
data=readString.substring(2);
Serial.print("AZ is: ");
Serial.println(data);
CD = data.toInt();
Serial.println(CD);
Serial.println();
}
if(readString.indexOf("EL") >=0) {
readString=readString.substring(2);
Serial.print("EL is: ");
Serial.println(readString);
CM = readString.toInt();
Serial.println(CM);
Serial.println();
}
if(readString.indexOf("CT") >=0) {
readString=readString.substring(2);
Serial.print("CT is: ");
Serial.println(readString);
CT = readString.toInt();
Serial.println(CT);
Serial.println();
}
if(readString.indexOf("CS") >=0) {
readString=readString.substring(2);
Serial.print("CS is: ");
Serial.println(readString);
CS = readString.toInt();
Serial.println(CS);
Serial.println();
}
if(readString.indexOf("BR") >=0) {
readString=readString.substring(2);
Serial.print("BR is: ");
Serial.println(readString);
BR = readString.toInt();
Serial.println(BR);
Serial.println();
}
//do some stuff
readString=""; //clears variable for new input
data="";
}
else {
readString += c; //makes the string readString
}
}
}