"class String' has no member named 'find'. startsWith?

Such usage as this is found in many older sketches:

if (sim800.find("+CMGS")) 

Most of the code compiles and runs, but sometimes, the compiler flags it as:
"class String' has no member named 'find'. Even after it has been compiled countless times. So I'm updating to index.

int index = text.indexOf(word);
  
  // Check if the word was found
  if (index >= 0) { // or (index != -1) {   //if (text.find("+CMGS")) {
    Serial.println("Word found");

Was 'find' once a member of String?

So then what about 'startsWith' as found in many code samples like this:

bool WaitForResponse()
{
  unsigned long startTime = millis();
  while (millis() - startTime < 5000)
  {
    String reply = ATCommandStream.readStringUntil('\n');
    if (reply.length() > 0)
    {
      Serial.print("Received: \"");
      Serial.print(reply);
      Serial.println("\"");
        if (reply.startsWith("OK"))
          return true;
        if (reply.startsWith("ERROR"))
          return false;
    }
  }
  Serial.println("Did not receive OK.");
  return false;
}

This also produces:

error: 'class SoftwareSerial' has no member named 'startsWith'

in some sketches, not all(?).
Thoughts?

Please post a link to such a sketch

error: 'class SoftwareSerial' has no member named 'startsWith'

Please post the full sketch that produces that error

Which Arduino board are you using ?

the Stream class and classes inheriting from Stream like SoftwareSerial

will respond to a call to find(). It means receive data on the Stream until you find the expected target (or it times out)). It's documented here

The String class does not implement find(). If you want to check if a substring appears in a String, then you use indexOf(). The String class does not inherit from Stream.

Ok,
It looks like find() is working after closing and restarting the IDE, except for this sketch.

//---- courtesy of John Wasser ---
//---- Sends "AT, AT+CMGF=1, AT+CMGS+"+17xxxxxxxx3", New Message"  all as a text? message  ??? ----
#include <SoftwareSerial.h>
  
SoftwareSerial ATCommandStream(3, 4);
unsigned long ATComandBaudRate = 9600;
const char * DestinationNumber = "17xxxxxxxx3";
const char * SMSMessage = "Test Message";

void setup()
{
  // start th serial communication with the host computer
  Serial.begin(9600);
  while (!Serial);
  delay(200);
  Serial.println("Sketch started.");
  ATCommandStream.begin(ATComandBaudRate);
  Serial.print("ATCommandStream started at baud rate ");
  Serial.println(ATComandBaudRate);
  delay(5000);
  // Set the SMS output to text mode
  SendShortCommand("AT+CMGF=1");
  SendSMSMessage(DestinationNumber, SMSMessage);
  SendShortCommand("AT"); // Just checking that it responds with OK
}

void loop() {}

bool WaitForResponse()
{
  unsigned long startTime = millis();
  while (millis() - startTime < 5000)
  {
    String reply = ATCommandStream.readStringUntil('\n');
    if (reply.length() > 0)
    {
      Serial.print("Received: \"");
      Serial.print(reply);
      Serial.println("\"");
        if (reply.find("+CMGS"))  // find() error here 
          return true;
        if (reply.startsWith("ERROR")) //startsWith() - no error
          return false;
    }
  }
  Serial.println("Did not receive OK.");
  return false;
}

bool SendShortCommand(String command)
{
  Serial.print("Sending command: \"");
  Serial.print(command);
  Serial.println("\"");

  ATCommandStream.print(command);
  ATCommandStream.print("\r\n");

  return WaitForResponse();
}

void SendSMSMessage(const char *number, const char *message)
{
  Serial.println("Sending SMS text");
  Serial.print("Sending: ");
  Serial.print("AT+CMGS=\"");
  Serial.print(number);
  Serial.println("\"(CR)");
  Serial.print(message); // The SMS text you want to send
  Serial.println("(EM)");

  ATCommandStream.print("AT+CMGS=\"");  // Send SMS
  ATCommandStream.print(number);
  ATCommandStream.print("\"\r"); // NOTE: Command ends with CR, not CRLF
  ATCommandStream.print(message); // The SMS text you want to send
  ATCommandStream.write(26); // ASCII EndMessage (EM) CTRL+Z character

  WaitForResponse();
}

Thank you

It would be helpful to see a sketch where find() works

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.