[轉貼] sscanf函數的進階用法

來源:互聯網
上載者:User
sscanf函數的進階用法 轉載時請註明出處:http://blog.csdn.net/absurd/ 大家都知道sscanf是一個很好用的函數,利用它可以從字串中取出整數、浮點數和字串等等。它的使用方法簡單,特別對於整數和浮點數來說。但新手可能並不知道處理字串時的一些進階用法,這裡做個簡要說明吧。  1.         常見用法。
charstr[512] = {0};         sscanf("123456 ", "%s", str);    printf("str=%s/n", str);
 2.         取指定長度的字串。如在下例中,取最大長度為4位元組的字串。
    sscanf("123456 ", "%4s", str);    printf("str=%s/n", str);
 3.         取到指定字元為止的字串。如在下例中,取遇到空格為止字串。
    sscanf("123456 abcdedf", "%[^ ]s", str);    printf("str=%s/n", str);
 4.         取僅包含指定字元集的字串。如在下例中,取僅包含1到9和小寫字母的字串。
    sscanf("123456abcdedfBCDEF", "%[1-9a-z]s", str);    printf("str=%s/n", str);
 5.         取到指定字元集為止的字串。如在下例中,取遇到大寫字母為止的字串。
    sscanf("123456abcdedfBCDEF", "%[^A-Z]s", str);    printf("str=%s/n", str);
 ~~~end~~

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.