In the programming process, it is sometimes necessary to split the string. The efficient use of these string-delimited functions will bring us a lot of convenience.
Below I will translate the strtok function that I learned in MSDN.
Strtok: Finds the next symbol in a string
Char *strtok (char *strtoken, const char *strdelimit);
Return value: Returns a pointer to the next symbol found in the Strtoken string, which returns NULL when the string cannot be found. per
The second call modifies the Strtoken string by replacing the delimiter encountered in the Strtoken string with a null character.
Parameters:
Strtoken: A string containing symbols
Strdelimit: Delimiter Collection
Note: When the Strtok function is called for the first time, the function ignores the spacing delimiter and returns the first character found in the Strtoken string
Number, and the symbol ends with a null character. By invoking a series of strtok functions, more symbols will be divided from the Strtoken string
Away. Each time the Strtok function is called, the Strtoken string is modified by inserting a null character after the symbol that is found. To
Reads the next symbol in the Strtoken, and when the Strtok function is called, the Strtoken parameter is null, which throws the Strtok function in the modified
The Strtoken string to find the next symbol.
Example (excerpt from MSDN)
/* STRTOK. C:in This program, a loop uses strtok
* To print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
Char string[] = "A string\tof, Tokens\nand some more tokens";
Char seps[] = ", \t\n";
Char *token;
void Main (void)
{
printf ("%s\n\ntokens:\n", string);
/* Establish string and get the first token: */
token = Strtok (string, SEPs);
while (token! = NULL)
{
/* While there is tokens in "string" */
printf ("%s\n", token);
/* Get Next token: */
token = Strtok (NULL, SEPs);
}
}
Output
A string of, tokens
and some more tokens
Tokens:
A
String
Of
Tokens
and
Some
More
Tokens
C language Split String function strtok