String Manipulation

By Kendro VillaruelKendro Villaruel, on 07 Apr 2013 07:02

Brief list and quick reference guide on string parsing used in Arduino programming. Also includes - String to Int, Int to String, String to CharArray, CharArray to String, CharArray to Int, Int to CharArray conversion.

1. indexOf()

This method gives you the ability to search for the first instance of a particular character value in a String.


String exampleString = "Bigfoot Arduino" // data type
int indexNumber = exampleString.indexOf('A');
Serial.print("Index Number:" + String(indexNumber));

Serial Monitor Output

Index Number: 8 // index count starts from 0.

For more info about indexOf() object, click here.
The link contains:
- Offset indexOf() - extract index of the second character (i.e. second 'A').
- lastIndexOf() - get the index number starting from the right most end of string.

2. length()

This string object outputs the length of the string.


String exampleString = "Bigfoot" // data type
int length = exampleString.length();
Serial.print("Length of String: " + String(length));

Serial Monitor Output

Length of String: 7

For more info about length() function, click here.
The link contains:
- trim() - delete all the white spaces in the string.

3. startsWith() and endsWith()

The String functions startsWith() and endsWith() allow you to check what character or substring a given String starts or ends with.


String exampleString = "Bigfoot Arduino"
   Serial.println("The string starts with 'BIG'.");
  Serial.println("The string ends with 'INO'!!");

Serial Monitor Output

The string starts with 'BIG'.
The string ends with 'INO'!!

For more info about length() function, click here.
The link contains:
- Offset startsWith - look for the startsWith at an offset position of string.

4. substring()

It allows you to look for an instance of a particular substring within a given String.


String exampleString = "abcdefghi";
Serial.println("Output 1: " + exampleString.substring(3));
Serial.println("Output 2: " + exampleString.substring(5,7));

Serial Monitor Output

Output 1: defghi
Output 2: fg

For more info about length() function, click here.

1. String to *char conversion

// Where:
// string is a string variable
// pointerString is *char variable
char *pointerString = new char[string.length() + 1];
strcpy(pointerString, string.c_str());
// your code
delete [] c_str;

- pointerString = string where pointerString is *char type. Be advised that you must have a decent amount of RAM left in
your sketch. As what I have experienced, if you have 500 RAM left, the code hangs..


2. char to *char conversion

char array[100];
char* pointer;
void setup()
    strcpy(pointer, array);

pointer (*char variable) will now have the same value as array (char variable).


3. Int to String conversion

String stringVariable;
int integerVariable = 12345;
stringVariable = String(integerVariable);

Yep. That easy.

4. String to Int Conversion

String stringVariable = "2000";
int integerVariable;
integerVariable = stringVariable.toInt();

Does not need any additional library includes!
**NOTE: ** toInt() function returns '0' if stringVariable to be converted is not a number type.

Add a New Comment