1, Introduction
char *strtok(char *str, const char *delim)
Function: it acts on the string str and takes the character in delim as the delimiter to cut str into a substring. This function returns the first substring decomposed. If there is no retrievable string, it returns a null pointer.
2, Instance
Example 1: basic usage of split string, split character "-", print split substring
#include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.runoob.com - website"; const char s[2] = "-"; char *token; /* Gets the first substring */ token = strtok(str, s); /* Continue to get other substrings */ while( token != NULL ) { printf( "%s\n", token ); token = strtok(NULL, s); } return(0); }
It should be noted here that when dividing a long string, the first parameter of the strtok function is passed into the string to be divided for the first call, and the first parameter of the strtok function should be passed into NULL for the second and subsequent calls. This is because when the first parameter of the strtok function is NULL, By default, this function uses the starting position of the last undivided string as the starting position of this segmentation until the end of the segmentation.
Operation results
Now, let's print the original str after segmentation,
#include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.runoob.com - website"; const char s[2] = "-"; char *token; /* Gets the first substring */ token = strtok(str, s); /* Continue to get other substrings */ while( token != NULL ) { printf( "%s\n", token ); token = strtok(NULL, s); } printf( "%s\n", str); return(0); }
Operation results
You think str becomes the first split string. Wrong. If we print like this, str changes again
#include <string.h> #include <stdio.h> int main() { char str[80] = "This is - www.runoob.com - website"; const char s[2] = "-"; char *token; /* Gets the first substring */ token = strtok(str, s); /* Continue to get other substrings */ while (token != NULL) { printf("%s\n", token); token = strtok(NULL, s); } printf("\n"); for (int i = 0; i < 34;i++){ printf("%c", str[i]); } return (0); }
Operation results
Originally, the change of the original string is to change the original position of the delimiter to '\ 0', but the content is still there.
Example 2
// Example 1 #include <string.h> void main() { char s[] = ".192.168.0.26"; const char *delim = "."; char *p; printf("%s ", strtok(s, delim)); while((p = strtok(NULL, delim))){ printf("%s ", p); } printf("\n"); }
If the first character of the string to be decomposed by the strtok function is a delimiter, the strtok function ignores the first character and counts directly from the next delimiter. For example: ". 192.168.0.26", the first character '.' will be ignored, and the first string after decomposition is "192".
Operation results
3, Summary
In a word, strtok function looks very simple, but there are many details to pay attention to when using it, so it should be understood and mastered. It should be noted that when using this function for string segmentation, the integrity of the decomposed string will be destroyed, and the s before and after call is different. After the first segmentation, the original string str is the first string after the segmentation, and the remaining strings are stored in a static variable. Therefore, an error will occur when multiple threads access the static variable at the same time. In many cases, instead of using this function, programmers implement their own string segmentation function to ensure thread safety,
strtok_s function
strtok_s is a split string security function under windows. Its function prototype is as follows:
char *strtok_s( char *strToken, const char *strDelimit, char **buf);
This function stores the remaining strings in buf variables instead of static variables to ensure security.
strtok_r function
strtok_s function is a security function for splitting strings under linux. The function declaration is as follows:
char *strtok_r(char *str, const char *delim, char **saveptr);
This function will also destroy the integrity of the string with decomposition, but it saves the remaining string in the saveptr variable to ensure security.
reference resources:
C library function – strtok() | rookie tutorial
Use of strtok() function and precautions_ sxy19930313 blog - CSDN blog_ Usage of strtok function
Detailed explanation of strtok() function_ Wei Bo - CSDN blogger - CSDN blog_ strtok