Sscanf function Advanced usage stickers + Add)

Source: Internet
Author: User

Advanced sscanf function usage (post + Add)

 

As we all know, sscanf is a very useful function that can be used to retrieve integers, floating-point numbers, and strings from strings. It is easy to use, especially for integers and floating-point numbers. However, beginners may not know some advanced usage when processing strings. Here is a brief description.

 

1.Common usage.

Char STR [512] = {0 };

Sscanf ("123456", "% s", STR );

Printf ("str = % s/n", STR );

 

2.Returns the string of the specified length.In the following example, a string with a maximum length of 4 bytes is obtained.

Sscanf ("123456", "% 4 s", STR );

Printf ("str = % s/n", STR );

 

3.The string that ends with the specified character.For example, in the following example, the string is obtained when a space is encountered.

Sscanf ("123456 abcdedf", "% [^]", STR );

Printf ("str = % s/n", STR );

 

4.Take a string that only contains the specified character set.For example, in the following example, take a string that only contains 1 to 9 letters and lowercase letters.

Sscanf ("123456 abcdedfbcdef", "% [1-9a-z]", STR );

Printf ("str = % s/n", STR );

 

5.Obtains the string of the specified character set.For example, in the following example, a string with uppercase letters is used.

Sscanf ("123456 abcdedfbcdef", "% [^A-Z]", STR );

Printf ("str = % s/n", STR );

 

Add:

6.Get the specified character and ignore other character strings.For example, in the following example, obtain the string to be located.

Sscanf ("Steven 23 Zhuhai", "% * s, % d, % s", age, city );

Printf ("str = % d/N", age );

 

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.