Find if a string begins with a specific set of characters in Arduino

ArduinoArduino BoardsArduino IDEArduino Programming Language

The startsWith() function in Arduino helps you determine if a string starts with a specific set of characters. It returns true if the string starts with the substring you specified, else it returns false. An example implementation is given below −


void setup() {
   // put your setup code here, to run once:
   String String1 = "AB_Test";
   String String2 = "CD_Test";
   String substr1 = "AB";
   String substr2 = "AB*";
         Serial.print("String1 starts with substr1");
         Serial.print("String2 starts with substr1");
         Serial.print("String1 starts with substr2");
         Serial.print("String2 starts with substr2");
void loop() {
   // put your main code here, to run repeatedly

As you can expect, this program will print "String1 starts with substr1" only. The corresponding Serial Monitor output is shown below (the garbage characters in the beginning are programming/ start-up noise) −


Updated on 24-Mar-2021 05:16:42