String startsWith and endsWith Functions

Check which characters/substrings a given string starts or ends with.

The String functions

startsWith()
and
endsWith()
allow you to check what character or substring a given String starts or ends with. They're basically special cases of
substring
.

Hardware Required

  • Arduino Board

Circuit

There is no circuit for this example, though your board must be connected to your computer via USB and the serial monitor window of the Arduino Software (IDE) should be open..

circuit

Code

startsWith()
and
endsWith()
can be used to look for a particular message header, or for a single character at the end of a String. They can also be used with an offset to look for a substring starting at a particular position. For example:

1stringOne = "HTTP/1.1 200 OK";
2if (stringOne.startsWith("200 OK", 9)) {
3Serial.println("Got an OK from the server");
4}

This is functionally the same as this:

1stringOne = "HTTP/1.1 200 OK";
2if (stringOne.substring(9) == "200 OK") {
3Serial.println("Got an OK from the server");
4}

Caution: If you look for a position that's outside the range of the string,you'll get unpredictable results. For example, in the example above stringOne.startsWith("200 OK", 16) wouldn't check against the String itself, but whatever is in memory just beyond it. For best results, make sure the index values you use for

startsWith
and
endsWith
are between 0 and the String's
length()
.

1/*
2 String startWith() and endsWith()
3
4 Examples of how to use startsWith() and endsWith() in a String
5
6 created 27 Jul 2010
7 modified 2 Apr 2012
8 by Tom Igoe
9
10 This example code is in the public domain.
11
12 http://www.arduino.cchttps://www.arduino.cc/en/Tutorial/StringStartsWithEndsWith
13*/
14
15void setup() {
16 // Open serial communications and wait for port to open:
17 Serial.begin(9600);
18 while (!Serial) {
19 ; // wait for serial port to connect. Needed for native USB port only
20 }
21
22 // send an intro:
23 Serial.println("\n\nString startsWith() and endsWith():");
24 Serial.println();
25}
26
27void loop() {
28 // startsWith() checks to see if a String starts with a particular substring:
29 String stringOne = "HTTP/1.1 200 OK";
30 Serial.println(stringOne);
31 if (stringOne.startsWith("HTTP/1.1")) {
32 Serial.println("Server's using http version 1.1");
33 }
34
35 // you can also look for startsWith() at an offset position in the string:
36 stringOne = "HTTP/1.1 200 OK";
37 if (stringOne.startsWith("200 OK", 9)) {
38 Serial.println("Got an OK from the server");
39 }
40
41 // endsWith() checks to see if a String ends with a particular character:
42 String sensorReading = "sensor = ";
43 sensorReading += analogRead(A0);
44 Serial.print(sensorReading);
45 if (sensorReading.endsWith("0")) {
46 Serial.println(". This reading is divisible by ten");
47 } else {
48 Serial.println(". This reading is not divisible by ten");
49 }
50
51 // do nothing while true:
52 while (true);
53}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/08/11 by SM

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.