Arduino

Arduino String endsWith() Function

In Arduino strings are widely used for handling data. Sometimes we need to check for a specific string inside the data received. So, Arduino provides a built-in function for this purpose called endsWith(). This article will cover what endsWith() function is and how we can use it in Arduino programming.

What is endsWith() Function in Arduino?

The endsWith() function is a built-in method in Arduino that allows checking whether a string ends with a specific character or sequence. The endsWith() function gives a Boolean output of true or false, depending on if a certain string ends with the specific character or sequence.

Syntax

The syntax of endsWith() function is as follows:

myString1.endsWith(myString2);

 

Here, myString1 is the string we want to check, and myString2 is the character or sequence we want to check for.

Parameters

This function contains following parameter:

  • myString1: This is a variable having type equal to string. This is the string we need to check.
  • myString2: Second parameter is also of type string. This is the string to be checked as a suffix of the calling string.

Return

This function gives us the Boolean value:

  • True: Function will return true if the calling string ends at specified suffix.
  • False: If the calling string does not end with the specified suffix.

How to Use endsWith() Function in Arduino?

Below Arduino code explain use of endsWith() function in Arduino programming:

void setup() {
  Serial.begin(9600);

  String str1 = "Linuxhint";
  String str2 = "Linux";
  Serial.print("Input String is: ");
  Serial.println(str1);
  if (str1.endsWith(str2)) {
    Serial.println("Input String ends with Linux");
  } else {
    Serial.println("Input String does not end with Linux");
  }
}

void loop() {
  // do nothing
}

 

In this example, we declare two String objects str1 and str2, and initialize them with the values “Linuxhint ” and “Linux“, respectively. We then use the endsWith() function to check if str1 ends with str2. Since the calling string str1 does not ends with the specified suffix that is str2, the output will be as shown below:

Let’s change str2 with “hint”, as string is ending with “hint”, the code would be:

void setup() {
  Serial.begin(9600);

  String str1 = "Linuxhint";
  String str2 = "hint";
  Serial.print("Input String is: ");
  Serial.println(str1);
  if (str1.endsWith(str2)) {
    Serial.println("Input String ends with hint");
  } else {
    Serial.println("Input String does not end with hint");
  }
}

void loop() {
  // do nothing
}

 

Conclusion

The endsWith() function in Arduino checks if a string ends at a specific character or sequence. By using this function, you can write more efficient and reliable code. For a detailed explanation of endsWith() function usage in Arduino read the article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.