C-language string segmentation

Source: Internet
Author: User
Tags strtok

it is very interesting to say that I think the C language is quite profound. But today, I only know that there is a strtok function. I tried it and suddenly felt how much repetitive work I had done before. Every time you need to parse the configuration file, and each time you need to split the string, you actually split the string by yourself, which is both tiring and error-prone. I will continue to learn more comprehensively! Here we reference A Piece Of strtok usage:

The strtok () function returns a pointer to the next "token" in Str1, Where Str2 Contains the delimiters that determine the token. strtok () returns Null If no token is found. In order to convert a string to tokens, the first call to strtok () shocould have Str1 Point to the string to be tokenized. All callafter this shocould have Str1 BeNull.

For example:

   Char Str[] = "Now # is the time for all # Good men to come to the # aid of their country";  
Char Delims[] = "#";
Char *Result = Null;
Result  = Strtok( Str, Delims );
While( Result ! = Null ) {
Printf ( "Result is \" % s \ "\ n", Result );
Result  = Strtok( Null, Delims );
}    

The above code will display the following output:

 Result Is "Now"  
Result  Is "Is the time for all"
Result  Is "Good men to come to"
Result  Is "Aid of their country"
 
This function is very similar to the lexical analysis in the compiler. It will solve many problems in future text processing. It seems necessary for me to systematically study C's library functions, not just the syntax and someAlgorithmTips. In this way, you can get twice the result with half the effort in your normal work.
 
Using this function, the configuration file shown below is very easy to parse:
 
Id1 value1 value2 value3
Id2 value1 value2 value3
 
...
 
Using this function makes it easier to split strings, for example, the following string to be split:
 
12 | 2345 | ASLD | alsfalskd
 
You only need to read the data to be processed and call strtok four times to parse the values of each row. In the past, I used sscanf instead of parsing it myself, but strtok is more suitable and flexible!

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.