Arduino strtok
-
Understanding the
strtok()
Function - Basic Example of strtok()
- Tokenizing a String with Multiple Delimiters
- Practical Use Case: Parsing User Input
- Conclusion
- FAQ

When working with strings in Arduino programming, one common task is to break down a string into smaller, manageable pieces. This process, known as tokenization, can be efficiently accomplished using the strtok()
function. Whether you’re parsing user input, processing data from sensors, or handling strings from serial communication, understanding how to use strtok()
can significantly enhance your projects.
In this article, we will explore the strtok()
function in-depth, providing clear examples and explanations to help you master string tokenization in your Arduino projects. Let’s dive into the world of strtok()
and see how it can simplify your coding experience.
Understanding the strtok()
Function
The strtok()
function is a part of the C standard library and is available in Arduino programming as well. It allows you to split a string into tokens based on specified delimiters. A token is essentially a substring that is separated by delimiters, which can be any character or set of characters you choose.
The function works in two main steps:
- Initialization: The first call to
strtok()
takes the string you want to tokenize and the delimiter(s) you want to use. - Token Retrieval: Subsequent calls to
strtok()
with aNULL
string will return the next token until no more tokens are found.
This two-step process is key to understanding how to use strtok()
effectively.
Basic Example of strtok()
Let’s start with a simple example of how to use strtok()
in an Arduino sketch. In this example, we will tokenize a comma-separated string.
#include <Arduino.h>
void setup() {
Serial.begin(9600);
char str[] = "Arduino,ESP32,NodeMCU";
char* token = strtok(str, ",");
while (token != NULL) {
Serial.println(token);
token = strtok(NULL, ",");
}
}
void loop() {
}
Output:
Arduino
ESP32
NodeMCU
In this code, we first include the Arduino library and set up the Serial communication. We define a string str
that contains three tokens separated by commas. The strtok()
function is called with str
and the delimiter ,
. The first token is printed, and then we enter a loop where we continue to call strtok(NULL, ",")
until no more tokens are available. This approach effectively breaks down the string into manageable parts that can be used in further processing.
Tokenizing a String with Multiple Delimiters
Sometimes, you might encounter strings that are separated by multiple delimiters. The strtok()
function can handle this scenario as well. Let’s see how to tokenize a string that uses both commas and spaces as delimiters.
#include <Arduino.h>
void setup() {
Serial.begin(9600);
char str[] = "Arduino, ESP32; NodeMCU: Raspberry Pi";
char* token = strtok(str, ", ;:");
while (token != NULL) {
Serial.println(token);
token = strtok(NULL, ", ;:");
}
}
void loop() {
}
Output:
Arduino
ESP32
NodeMCU
Raspberry Pi
In this example, we define a string str
that contains tokens separated by commas, spaces, semicolons, and colons. By passing ", ;:"
as the delimiter to strtok()
, we can split the string into individual tokens regardless of which delimiter is used. This flexibility makes strtok()
a powerful tool for string manipulation in Arduino programming.
Practical Use Case: Parsing User Input
Tokenization is particularly useful when parsing user input. Imagine you have a scenario where a user enters a command followed by parameters, all separated by spaces. You can use strtok()
to extract the command and its parameters easily.
#include <Arduino.h>
void setup() {
Serial.begin(9600);
char input[] = "LED ON 255";
char* command = strtok(input, " ");
while (command != NULL) {
Serial.println(command);
command = strtok(NULL, " ");
}
}
void loop() {
}
Output:
LED
ON
255
In this code, we simulate user input as a string. The first call to strtok()
retrieves the command, and subsequent calls retrieve the parameters. This approach allows you to process commands dynamically, making your Arduino project more interactive and responsive to user input.
Conclusion
The strtok()
function is an invaluable tool for anyone working with strings in Arduino programming. By understanding how to use this function effectively, you can break down complex strings into manageable tokens, making your code cleaner and more efficient. Whether you’re parsing user input, processing data, or handling strings from various sources, mastering strtok()
will undoubtedly enhance your Arduino projects. So, the next time you find yourself needing to manipulate strings, remember the power of strtok()
.
FAQ
-
What is the purpose of the
strtok()
function?
thestrtok()
function is used to tokenize a string by splitting it into smaller substrings based on specified delimiters. -
Can strtok() handle multiple delimiters?
Yes, strtok() can handle multiple delimiters by passing a string of characters as delimiters.
-
What happens if strtok() is called with NULL?
Calling strtok() with NULL continues tokenization from the last string processed, returning the next token. -
Is strtok() safe to use with constant strings?
No, strtok() modifies the original string, so it should not be used with constant strings. -
How can I reset the tokenization process?
To reset the tokenization, you need to call strtok() again with the original string.