Ah, so String is based on string. I assumed they were different as peoples have highlighted an OP's post asking when they say string did they mean String or string.
Hi there - the current source string is a String (capital S), the one i should not be using according to PaulS
I'll try to follow what dhenry said, but especially the "search for '+' til the end of the source string;" is something i am not sure about how to do yet.
A String object wraps a NULL terminated char array. That is not quite the same thing as being based on a string.
Typically, when we ask of the OP means string or String, it is because they say one and then post some code that uses the other, or they don't post any/adequate code to make it possible to determine.
The correct way to parse those lines is to parse them!
That means you need to count ',' separators (but ignore them when inside string quotes). This will get you to the correct
column, and you can extract the string-quote delimited string from there.
Unfortunately proper parsing is often quite complex (string quotes can be escaped inside a string for instance) so
on a tiny little microcontroller its tempting to take shortcuts... Can come back to bite you later though.
a quick hack to extract with 2 pointers, one to find the start and one to find the end of the string
char s[] = "+CMGR: \"REC UNREAD\",\"+8613920001234\",\"\",\"12/12/13,15:08:55+50\""; // note that the \" are escaped with a \\..
char t[20];
void setup()
{
int len = strip(s, t);
Serial.begin(9600);
Serial.println("start");
Serial.println(len);
Serial.print(t);
}
void loop()
{}
// dedicated function to strip second field which is ... not so elegant ...
int strip(char * in, char * out)
{
char *p = in;
// find start of second field
while (',' != *p++);
if (*p =='\"') p++;
// find endpoint
char *q = p+1;
while ('\"' != *q++); // should also test for end of string ... first ...
q--; //we know we have gone 1 byte to far
int len = q - p; // warning pointer math
strncpy(out, p, len);
return len;
}
Below has the various methods to slice/dice a String. What method you use depends on how the string is constructed and how consistant the data is inside the String. The number starts after the second + in the string, so that might be a start locator for capturing the number string. Probably several interesting ways to solve the issue. The substring function might be of interest.
As part of a larger project I'm writing a "string" object (note "object" not "class", it's all C not C++) and one of the methods extracts a field of variable length at a variable location. You just supply the delimiters.
So I thought I'd plug your string into it and see if it worked.
All of what I'm doing is for an ARM but that said this sort of function is just C so should run on anything and I plan to port it across to the Arduino one day.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
String finalstring;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
finalstring = readString.substring(21, 35);
Serial.println(finalstring);
readString="";
finalstring="";
}
}