A summary of the split string of C-language string operations under Linux __linux

Source: Internet
Author: User
Tags strtok

How to split strings in C/cpp, similar to the Split function

1, split string
Char *strtok (char *str, const char *delim);
Function: Explode string as a set of strings. STR is the string to be decomposed, Delim as a delimiter string. Essentially, Strtok looks for characters contained in Delim in STR and replaces it with null (' \0′ ') until the entire string is searched.
Description: At the first invocation, Str points to the string to be exploded, and then calls again to set the STR to NULL. Strtok finds the characters contained in Delim in STR and replaces them with null (' \0′ ') until the entire string is searched.
Return value: A split string that starts at the beginning of the Str. Returns NULL when a string is not split. All the characters contained in the Delim are filtered out, and the filtered area is set to a segmented node.
Example:

1. #include <stdio.h>

2. #include <string.h>

3.

4. int main () {

5.//id, name, Chinese, maths, English

6. charstr[]= "2, John, 89,99,66";

7. Char*token=strtok (str, ",");

8. while (Token!=null) {

9. printf ("%s\t", token);

Token=strtok (NULL, ",");

11.}

printf ("\ n");

return 0;

14.}

Sample program Output:

1.2 Sheets 389 99 66

Description: The str parameter must be in the form of an array, not a string constant (for example: Char *str= "2, John, 89,99,66"), because the strtok will modify STR during execution and must ensure that STR is writable.

2, split string (or split string)
Char *strsep (char **stringp, const char *delim);

1. #include <stdio.h>

2. #include <string.h>

3.

4. int main () {

5.//id, name, Chinese, maths, English

6. charstr[]= "2, John, 89,99,66";

7.//str is a pointer constant, and the first parameter of the STRSEP requires a pointer to the pointer, so it is not possible to perform an address operation on STR,

8.//Here again define a pointer variable can take address operation. Otherwise, there will be a segment error.

9. Char *strv=str;

Char*token=strsep (&STRV, ",");

One. while (Token!=null) {

printf ("%s\t", token);

Token=strsep (&STRV, ",");

14.}

printf ("\ n");

return 0;

17.}

Note: This function also modifies the contents of the first parameter, so you must guarantee that the supplied is not a string constant.


Resources:

Http://biancheng.dnbcw.info/c/452773.html

Http://biancheng.dnbcw.info/c/452774.html

http://www.oschina.net/code/snippet_2325404_47570

http://blog.csdn.net/bg2bkk/article/details/37569555 Reprint Address: https://blog.csdn.net/benpaobagzb/article/details/50719197

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.