Splitting a string in Arduino
substring()
This tutorial will discuss splitting a string using the function in Arduino .
substring()
Splitting a string in Arduino using the function
Arduino provides an inbuilt function substring()
to split a given string. We can split the string using the start and end index values.
substring()
The function takes two parameters. The first parameter is the starting index value from where we want to start the splitting process and the second parameter is the ending index value at which the splitting process will stop.
grammar:
Sub_string = MyString.substring(from, to);
The variable Sub_string
will contain substring()
the output of the function, while MyString
the variable will contain the original string we want to split. from
The variable contains the starting index, and to
the variable contains the ending index.
Let's define a string and substring()
split it using the function and then print it on the Arduino's serial monitor.
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, Serial.println()
the function prints the result on the serial monitor of Arduino. The string split will start at 0, including the 0 index character, and end at index 5, excluding the character at index 5.
We can also indexOf()
find the character index using Arduino's index function. The index function accepts two parameters.
The first parameter is mandatory and indicates the character or string whose index we want to find. The second parameter is optional and it indicates the starting index from which to find the character index.
By default, indexOf()
the function searches the string from the beginning to find the index of a given character, but we can also indexOf()
pass an index as a starting point using the second argument of the function.
grammar:
int index = MyString.indexOf(val, from);
index
The variable will store val
the variable index which contains the character or string in the above code. from
The variable defines the starting index which is used as the starting point to find the given character index.
substring()
We use the function when we don't know the index of the character to pass into indexOf()
.
For example, if we want to split a given string using the space character as the ending index, we can use indexOf()
the function to find the index of the space character and then substring()
use it in the function to split the string.
If the index is not found in the given string, indexOf()
the function will return -1.
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 display the index and the result of the string split on the serial monitor window.
We can also split the string based on the number of lines.
For example, if a string contains multiple lines of text, we want to split and treat each line as a separate string.
We can use indexOf('\n')
the function to find the index of the new line and split the given string accordingly.
indexOf()
The function starts searching for the index from the beginning of the given string. However, we can also use lastIndexOf()
the function, which starts searching for the index from the end of the string.
As a second parameter, we can also lastIndexOf()
pass the starting index in the 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 have used the split function in the above code lastIndexOf()
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 is the result of the string split.
We use length()
the function to get the length of the given string, which will be used as substring()
the ending index value in . We use delay()
the function to add some delay in the code execution as the loop will repeat the process.
If we only want to do the splitting, we can setup()
write the code in a function, which will only run once.
We can split a string based on any character. We just need to indexOf()
find its index using the split_string function.
We can substring()
split a string using the split function. Suppose we want to get all the lines in a given string separately. We have to get one line, save the remaining string in a variable, and then do the same thing again using a loop until all the lines are extracted.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Stopping a loop in Arduino
Publish Date:2025/04/16 Views:150 Category:C++
-
This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi
Arduino prints to console
Publish Date:2025/04/16 Views:68 Category:C++
-
This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything
Arduino Array Length
Publish Date:2025/04/16 Views:181 Category:C++
-
Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p
Arduino 2D Array
Publish Date:2025/04/16 Views:108 Category:C++
-
In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional
Printing Character Array in Arduino
Publish Date:2025/04/16 Views:59 Category:C++
-
This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element
Arduino Square Wave Generator
Publish Date:2025/04/16 Views:181 Category:C++
-
digitalWrite() This tutorial will discuss generating a square wave using the function in Arduino . Arduino Square Wave Generator A square wave consists of maximum and minimum values, and the transitions between these values are instan
Arduino mills() function
Publish Date:2025/04/16 Views:156 Category:C++
-
This tutorial will discuss millis() the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis() the functions. millis() Checking the elapsed time in Arduino using th
在 Arduino 中拆分字符串
Publish Date:2024/03/13 Views:150 Category:C++
-
可以使用 Arduino 中的 substring() 函数拆分字符串。
在C中将整数转换为字符
Publish Date:2024/01/03 Views:135 Category:C语言
-
本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。