Does anybody have a basic sample code / reference on how to parse a String ( not char array) to extract numeric characters only.
I can use indexOf to get single character, but would like to be able to find any non-numeric character.
Any constructive suggestions would be appreciated.
What type of string data are you dealing with? You might make character test to see if the character has a decimal value greater than 47 and less than 58. If you have control of how the data is being sent, then it usually is easier to format the sent data such that parsing is simpler.
The String indexOf method can identify only single character position.
But I have to specify /code the character first.
What I want is exactly what you suggesting - verify that the character range is outside numeric characters.
To my knowledge String does not have method to identify range of characters, but Stream find looks as it could be used.
This is no big issue, I could just make sure that the valid characters in the String are separated / delimited by one and only one non numeric character and force the user to retype the string again if not.
It is all manual ( terminal ) input of data.
Just trying to add some safeguards, but I am sure I could just check the characters as they are recieved and not wait until the entire string is in. Reason for checking the entire string is because it was originally machine generated and the delimiter character was only one.
Cheers Vaclav
The String indexOf method can identify only single character position
Well, that is not exactly correct. The indexOf can locate character strings in a String. Bottom is code that looks for specific character strings. Below is more info.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
//A very simple example of sending a string of characters
//from the serial monitor, capturing the individual
//characters into a String, then evaluating the contents
//of the String to possibly perform an action (on/off board LED).
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
Thanks, I need to digest this.
Basically same as CString (MFC) class string within a string, right?
I have decided to analyze the characters as they are received instead of getting the whole string and than analyzing it.
Believe it or not I cannot get plain binary from the ASCII code received, ea #1 is 31 (ASCII)but I also get 49.
I am working on that for now. I'll get back to you when I figure this silly error.
Cheers Vaclav
The String indexOf method can identify only single character position
Well, that is not exactly correct. The indexOf can locate character strings in a String. Bottom is code that looks for specific character strings. Below is more info.
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
//A very simple example of sending a string of characters
//from the serial monitor, capturing the individual
//characters into a String, then evaluating the contents
//of the String to possibly perform an action (on/off board LED).
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
I did change my code and now checking the characters as they come. It is less convoluted IMHO.
I will try this later, but I suspect it would not do because it looks for match of the substring, not individual characters in it.
But I may have it confused with C++ (MFC).
Anyway, thanks for your suggestion.
Cheers Vaclav