C-Language split string

Source: Internet
Author: User

Recently need to use C language to implement a split string function, the separator is "=", and then get = Both sides of the data

After using Java habits to write C language code always want to call the function, but there is no split function in C, for the beginning of the string segmentation is the most stupid way is based on the equal sign in the string position and then use the strncpy function interception, but this interception has a great disadvantage, when the need to intercept the string more, The position of the equal sign is not fixed, so the intercept string does not achieve the desired effect, and later found a very useful function in C sscanf. See the code below.

#include <stdio.h>int main () {printf ("hello\n"), char *test = "aaa=sss"; char Key[4];char value[40];sscanf (test, "% [^=]=%s ", Key,value);p rintf ("%s\n ", key);p rintf ("%s\n ", value); return 0;}
This can be obtained = The data on both sides is stored in key and value, respectively.


C-Language split string

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.