Split String in Arduino

Ammar Ali Dec 21, 2022 Apr 12, 2022
Split String in Arduino

This tutorial will discuss splitting a string using the substring() function in Arduino.

Use the substring() Function to Split a String in Arduino

Arduino provides a built-in function substring() to split a given string. We can split a string using the start and end index value.

The substring() function has two arguments. The first argument is the starting index value from which we want to start the splitting process, and the second is the ending index value where the splitting process will stop.

Syntax:

Sub_string = MyString.substring(from, to);

The variable Sub_string will contain the output of the substring() function, and the MyString variable will contain the original string we want to split. The from variable contains the starting index, and the to variable contains the ending index.

Let’s define a string and split it using the substring() function and print it on the serial monitor of Arduino.

Example:

String My_S = "hello world";

void setup(){
    Serial.begin(9600);
}
void loop(){
    String sub_S = My_S.substring(0,5);
    Serial.println(sub_S);
    delay(1000);
}

Output:

hello

In the above code, the Serial.println() function prints the result on the serial monitor of Arduino. The string splitting will start from 0, include the 0 index character, and end at index 5, excluding the character at index 5.

We can also find the character index using the indexOf() function of Arduino. The index of function accepts two arguments.

The first argument is mandatory, representing the char or string whose index we want to find. The second argument is optional, and it represents the starting index to find the index of the character.

By default, the indexOf() function starts searching the string from the start to find the index of the given character, but we can also pass an index as a starting point using the second argument of the indexOf() function.

Syntax:

int index = MyString.indexOf(val, from);

The index variable will store the val variable index, which contains a character or string in the above code. The from variable defines the starting index used as the starting point to find the index of the given character.

We use the indexOf() function when we do not know the character index to pass in substring().

For example, if we want to split the given string using the space character as the ending index, we can use the indexOf() function to find the index of the space character and then use it inside the substring() function to split the string.

The indexOf() function will return -1 if the index is not found in the given string.

Example:

String My_S = "hello world";

void setup(){
  Serial.begin(9600);
}
void loop(){
    int index = My_S.indexOf(' ');
    String sub_S = My_S.substring(0,index);
    Serial.println(index);
    Serial.println(sub_S);
    delay(1000);
}

Output:

5
hello

In the above code, the variable index contains the index of the space character present in the given string. As you can see, we displayed the index and the result of string splitting on the serial monitor window.

We can also split a string based on the number of lines.

For example, if a string contains multiple lines of text and we want to split and get each line as a separate string.

We can use the indexOf('\n') function to find the index of a new line and split the given string accordingly.

The indexOf() function starts searching for the index from the start of the given string. However, we can also use the lastIndexOf() function, which searches for the index starting from the end of a string.

As the second argument, we can also pass the starting index inside the lastIndexOf() function. For example, let’s split a string containing three lines, get the last line, and display it on the serial monitor window.

Example:

String My_S = "hello world\nGreetings\nPeople";

void setup(){
    Serial.begin(9600);
    Serial.println(My_S);
}
void loop(){
    int index = My_S.lastIndexOf('\n');
    int length = My_S.length();
    String sub_S = My_S.substring(index, length);
    Serial.println(sub_S);
    delay(100000);
}

Output:

hello world
Greetings
People

People

We used the lastIndexOf() function in the above code because we only want to get the last line of text present in the given string. The first three lines are the given string in the output, and the fourth line results from string splitting.

We used the length() function to get the length of the given string which will be used as the ending index value inside substring(). We used the delay() function to add some delay in the code execution because the loop will repeat the process.

If we only want to do the splitting one, we can write the code inside the setup() function, which only runs once.

We can split a string based on any character. We only have to find its index using the indexOf() function.

We can split the string using the substring() function. Suppose we want to get all the lines separately in a given string. We have to get one line, save the remaining string in a variable, and perform the same operation again using a loop until all the lines have been extracted.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - Arduino String