Split a String Arduino
How to split string arduino from the serial – String is a class used to store text. A String variable containing text enclosed in quotation marks. Example: x = “Hello”;
What is Split String?
When serial data communication, the data that we transmit or receive may be contains a text.
If in our program there is some logic such as “IF” on the incoming serial data, we have to split the data.
In various programming languages, separating this text can be found such as split string in python, java, javascript, etc.
For example, in our program there is a logic to turn on the RGB LED with two commands, “Color” and “Duration” of the LED on.
So, the data received is in the format “Color. Time;”. Every command that will be received ends with a point “.”.
When Arduino receives data “Red.1000;” then the LED will turn on red and will turn off after 1 second.
From the example above, we are split the text with the boundary is a point “.”
The simple logic is accept the first data up to the point “.” and save it in the 1st Array, then read the second data until the point “.” and store it in the 2nd Array, and so on.
How to split strings?
The methods for separating this text are:
- The program will count how many characters are contained in the text.
- The program will read one by one character and save the characters into the Array to 0.
- When it finds a dot character “.”, The program will stop saving to the 0 array.
- The program will read the characters after the period “.” and save it to the 1st Array.
- etc.
I made a small sample program. Upload this program to Arduino, open the serial monitor. Type a word (whatever) and separate it with a period of “.”.
For example, I send “Oliver Manuel (2001). Origin of Elements in the Solar System”. Source of this words is wikipedia.
Split String Code
// Set the incoming data to 10 data
String ab[10];
String data;
String sectionData;
int a;
int stringData;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
data = Serial.readString();
for (a = 0; a < data.length() - 1; a++)
{
// split data based on point (.), Can also be replaced by comma (,)
ab[a] = getData(data, '.', a);
if (ab[a] != NULL)
{
Serial.print("Array ");
Serial.print(a);
Serial.print(" = ");
Serial.println(ab[a]);
}
}
}
}
String getData(String data, char delimiter, int sequence)
{
stringData = 0;
sectionData = "";
for (int i = 0; i < data.length() - 1; i++)
{
if (data[i] == delimiter)
{
stringData++;
}
else if (stringData == sequence)
{
sectionData.concat(data[i]);
}
else if (stringData > sequence)
{
return sectionData;
break;
}
}
return sectionData;
}

You can replace the point ‘.’ with what you like. For example a semicolon ‘;’. On the line “ab[a] = getData(data, ‘.’, a);” to “ab[a] = getData(data, ‘;’, a);”
I tested the program above using Arduino Pro Mini. You can most likely use it on other Arduino variants.
Hopefully this article is useful. End.
Search Terms:
- arduino split string by comma
- substring arduino
- arduino split string into char array
- arduino split char array
- arduino split string by newline
1 comment