C language Split String function strtok

Source: Internet
Author: User
Tags strtok

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.