find string in char array

Hello all!

I am going round in circles trying to determin if an output char array (from a GSM module serial) contains a certain telephone number.

I tried this with the car array "buffer" containing up to 64 chars from a GPRS.read() operation.

String aPhone1 = "07676438";
String bufferString(buffer);
int posInStr = bufferString.indexOf(aPhone1);

this does not work - so I am looking for an operation to convert "buffer" to a string bufferString, so I can use the .indexOf method.

Can anyone help?

beejayf:
Hello all!
Can anyone help?

With nullterminated strings (char arrays) always use the strstr() function to find a nullterminated string and its position within another nullterminated string.

so I am looking for an operation to convert “buffer” to a string bufferString, so I can use the .indexOf method.

Not going to help you with that. There are functions to determine if a string (a NULL terminated array of chars) contains a string. Use them, instead. ANY book on C will show all of the string handling functions, so you never need to use the String crutches again.

Here are the most commom functions.

here is an example (run it with your console set at 115200 bauds)

const char* GSMbuffer = "12345678901234567-->07676438<--1234567890";
const char* phoneNumber = "07676438";

void setup() {
  char * p;
  Serial.begin(115200);
  p = strstr (GSMbuffer, phoneNumber);
  if (p) {
    Serial.print("found ["); Serial.print(phoneNumber); Serial.print("] at position "); Serial.print((int) (p - GSMbuffer));
  } else {
    Serial.print("could not find ["); Serial.print(phoneNumber); Serial.print("]");
  }
}

void loop() {}
1 Like