String Object in Arduino

String Object Constructors The String object allows you to manipulate strings of text in a variety of useful ways. You can append characters to Strings, combine Strings through concatenation, get the length of a String, search and replace substrings, and more. This tutorial shows you how to initialize String objects.
String stringOne = "Hello String"; using a constant String
String stringOne = String('a'); converting a constant char into a String
String stringTwo = String("This is a string"); converting a constant string into a String object
String stringOne = String(stringTwo + " with more"); using a constant integer
String stringOne = String(13); using an int and a base
String stringOne = String(analogRead(0), DEC); using an int and a base (binary)
String stringOne = String(millis(), DEC);M using a long and a base
String stringOne = String(5.698, 3);using a float and the decimal places

String Appending Operators

Just as you can concatenate Strings with other data objects using the StringAdditionOperator, you can also use the += operator and the concat() method to append things to Strings. The += operator and the concat() method work the same way, it's just a matter of which style you prefer. The two examples below illustrate both, and result in the same String:

String stringOne = "A long "; // using += to add a long variable to a string: stringOne += "interger"; or String stringTwo = "A long "; // using concat() to add a long variable to a string: stringTwo.concat("integer");

String Comparison Operators

The String comparison operators ==, !=,>, < ,>=, <= , and the equals() and equalsIgnoreCase() methods allow you to make alphabetic comparisons between Strings. They're useful for sorting and alphabetizing, among other things.

The operator == and the method equals() perform identically. In other words

if (stringOne.equals(stringTwo)) { is identical to if (stringOne ==stringTwo) {
The ">" (greater than) and "<" (less than) operators evaluate strings in alphabetical order, on the first character where the two differ. So, for example "a" < "b" and "1" < "2", but "999" > "1000" because 9 comes after 1. Caution: String comparison operators can be confusing when you're comparing numeric strings, because the numbers are treated as strings and not as numbers. If you need to compare numbers, compare them as ints, floats, or longs, and not as Strings.


It allows you to look for an instance of a particular substring within a given String.
String stringOne = "Content-Type: text/html"; Serial.println(stringOne); // substring(index) looks for the substring from the index position to the end: if (stringOne.substring(19) == "html") { Serial.println("It's an html file"); } // you can also look for a substring in the middle of a string: if (stringOne.substring(14, 18) == "text") { Serial.println("It's a text-based file");

String indexOf() and lastIndexOf() Method

The String object indexOf() method gives you the ability to search for the first instance of a particular character value in a String. You can also look for the first instance of the character after a given offset. The lastIndexOf() method lets you do the same things from the end of a String.

String stringOne = ""; int firstClosingBracket = stringOne.indexOf('>');

In this case, firstClosingBracket equals 5, because the first > character is at position 5 in the String (counting the first character as 0). If you want to get the second closing bracket, you can use the fact that you know the position of the first one, and search from firstClosingBracket + 1 as the offset, like so:

stringOne = ""; int secondClosingBracket = stringOne.indexOf('>', firstClosingBracket + 1 ); The result would be 11, the position of the closing bracket for the HEAD tag. If you want to search from the end of the String, you can use the lastIndexOf() method instead. This function returns the position of the last occurrence of a given character.

stringOne = ""; int lastOpeningBracket = stringOne.lastIndexOf('<');

In this case, lastOpeningBracket equals 12, the position of the < for the BODY tag. If you want the opening bracket for the HEAD tag, it would be at stringOne.lastIndexOf('<', lastOpeningBracket -1), or 6.

String length()

We can get the length of Strings using the length() command.

String to Int Function

The toInt() function allows you to convert a String to an integer number.

while (Serial.available() > 0) { int inChar =; if (isDigit(inChar)) { // convert the incoming byte to a char and add it to the string: inString += (char)inChar; } // if you get a newline, print the string, then the string's value: if (inChar == '\n') { Serial.print("Value:"); Serial.println(inString.toInt()); Serial.print("String: "); Serial.println(inString); // clear the string for new input: inString = ""; } }