Home  >  Article  >  Detailed explanation of strtok function usage

Detailed explanation of strtok function usage

zbt
zbtOriginal
2023-11-28 10:15:591896browse

The strtok function is a function used to split a string according to a specified delimiter. The split substrings can be returned one by one through multiple calls. It should be noted that the original string will be modified by the strtok function, which will replace the delimiter with the string terminator '\0', so the original string may have been modified after all substrings have been processed. If you need to keep the original string, you can create a copy before splitting.

Detailed explanation of strtok function usage

The strtok function is a string splitting function in C language. It is used to split a string according to the specified delimiter and return the split sub-characters. string. Its usage is as follows:

char *strtok(char *str, const char *delimiters)

str: ​​The string to be split, the string to be split is passed in when called for the first time, and NULL is passed in in subsequent calls.

delimiters: delimiter string, specifying the set of characters used to split the string.

The strtok function works as follows:

1. When calling for the first time, pass in the string to be split as a parameter and return the pointer of the first substring.

2. In subsequent calls, if NULL is passed in as the first parameter, the function will continue to split the string from the end of the previous call and return the pointer to the next substring.

3. When there are no more substrings to return, the function returns NULL.

The following is a simple example demonstrating the usage of strtok function:

#include
#include
int main() {
char str[] = "Hello, World! How are you?";
const char delimiters[] = " ,!";
// 第一次调用strtok
char *token = strtok(str, delimiters);
// 通过循环继续分割字符串
while (token != NULL) {
printf("%s\n", token);
// 后续调用strtok
token = strtok(NULL, delimiters);
}
return 0;
}

In the above example, we convert the string "Hello, World! How are you?" split according to spaces, commas and exclamation marks. First, call the strtok function for the first time and pass in the string to be split and the delimiter string. The function returns the pointer "Hello" of the first substring, and then Continue to call the strtok function through the loop, passing in NULL as the first parameter, and continue to split the string. Each time through the loop, the function returns the pointer to the next substring, until there are no more substrings to return, the function returns NULL, the loop ends.

It should be noted that the original string will be modified by the strtok function, which will replace the delimiter with the string terminator '\0', so after all substrings are processed , the original string may have been modified. If you need to keep the original string, you can create a copy before splitting.

In summary, the strtok function is a function that is used to divide the string according to the specified delimiter. The split function can return the split substrings one by one through multiple calls. By understanding the usage and precautions of the strtok function, you can easily handle the need for string splitting.

The above is the detailed content of Detailed explanation of strtok function usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn