[Excerpt] use the strtok function in C to separate strings

Source: Internet
Author: User
Tags strtok

Unlike advanced languages such as Java and PHP, C language directly encapsulates string processing functions in objects. Common string processing in the C language often causes us to be overwhelmed ...... Fortunately, the C language provides powerful string processing functions such as strtok, which can help us implement some of the required functions. Next we will introduce the usage of strtok functions and specific examples.

Original Type: char * strtok (char * s, char * delim );
Function: break down a string into a group of strings. S is the string to be decomposed, and delim is the separator string. Essentially, strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.
Description: During the first call, s points to the string to be decomposed, and then calls it again to set S to null. Strtok searches for characters contained in delim in S and replaces them with null ('\ 0') until the entire string is searched.
Return Value: Split strings starting with S. If no split string exists, null is returned. All delim characters are filtered out, And the filtered characters are set as a separated node.

Example:

Download: strtok_sample.c
  1. # Include<Stdio. h>
  2. # Include<String. h>
  3. IntMain(IntArgc,Char**Argv)
  4. {
  5. Char*Buf1="Aaa, A, BBB-C, ee | ABC";
  6. /* Establish string and get the first token :*/
  7. Char*Token=Strtok( Buf1,",-|");
  8. While( Token! =Null)
  9. {
  10. /* While there are tokens in "string "*/
  11. Printf( "% S",Token );
  12. /* Get next token :*/
  13. Token=Strtok(Null,",-|");
  14. }
  15. Return 0;
  16. }

From: http://blog.minidx.com/2008/02/03/469.html

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.