C/C++字串尋找函數

來源:互聯網
上載者:User

C/C++ string庫(string.h)提供了幾個字串尋找函數,如下:

memchr 在指定記憶體裡定位給定字元
strchr 在指定字串裡定位給定字元
strcspn 返回在字串str1裡找到字串str2裡的任意一個字元之前已尋找的字元數量
strrchr 在字串裡定位給定字元最後一次出現的位置
strpbrk 在字串str1裡定位字串str2裡任意一個首次出現的字元
strspn 返回字串str1從開始字元到第一個不在str2中的字元個數
strstr 在字串str1中定位字串str2首次出現的位置

函數說明:

1、memchr

原型:const void * memchr ( const void * ptr, int value, size_t num);

            void * memchr ( void * ptr, int value, size_t num);

參數:ptr,待尋找的記憶體指標;

            value,要尋找的值;

            num,記憶體大小。

說明:在ptr指向的記憶體中尋找value,返回指向和value最初相符的元素指標,如果沒有找到,返回NULL指標。

2、strchr

原型:const char * strchr ( const char * str, int character);

            char * strchr ( char * str, int character);

參數:str,待尋找字串指標;

            character,要尋找的值。

說明:在字串str中尋找character,返回指向和character最初相符的元素指標,如果沒有找到,返回NULL指標。

3、strcspn

原型:size_t strcspn ( const char * str1, const char * str2);

參數:str1,待尋找的字串指標;

            str2,要尋找的字串指標。

說明:在str1中尋找str2中的字元,返回找到str2中任意一個字元時已尋找的字元個數。

4、strrchr

原型:const char * strrchr ( const char * str, int character);

            char * strrchr ( char * str, int character);

參數:str,待尋找的字串指標;

            character,要尋找的值。

說明:在str中尋找character,返回指向與character最後一次匹配的元素的指標,如果沒有找到,返回NULL指標。

5、strpbrk

原型:const char * strpbrk (const char * str1, const char * str2);

            char * strpbrk ( char * str1, const char * str2);

參數:在str1中尋找str2中任意一個字元,返回指向與首次出現的str2中的任意一個字元匹配的元素的指標,如果沒有找到,返回NULL指標。

6、strspn

原型:size_t strspn ( const char * str1, const char * str2);

參數:str1,待尋找的字串指標;

            str2,要尋找的字串指標。

說明:從str1的第一個字元開始算起,直到出現一個不在str2中的字元為止,返回字元個數。

7、strstr

原型:const char * strstr ( const char * str1, cosnt char *str2);

            char * strstr ( char * str1, const char * str2);

參數:str1,待尋找的字串指標;

            str2,要尋找的字串指標。

說明:在str1中尋找匹配str2的子串,並返回指向最初相符時的第一個元素指標。如果沒有找到,返回NULL指標。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.