複製鏈接
請複製以下鏈接發送給好友

strcspn

鎖定
C語言裏面字符串函數之一,用於查找子串,詳見正文。
外文名
strcspn
語    種
C/C++
頭文件
string.h

目錄

strcspn簡介

原型:size_t strcspn(const char *s, const char * reject);
相關頭文件:string.h
strcspn是由string complementary span的縮寫組成,表示獲取字符串s起始位置起字符不在reject的跨度(長度) [1] 

strcspn功能

函數説明:strcspn()從參數s 字符串的開頭計算連續的字符,而這些字符都完全不在參數reject 所指的字符串中。簡單地説, 若strcspn()返回的數值為n,則代表字符串s 開頭連續有n 個字符都不含字符串reject 內的字符。
返回值:返回字符串s 開頭連續不含字符串reject 內的字符數目。

strcspn舉例

#include <string.h>

main()

{

    char *str = "Linux was first developed for 386/486-based pcs. ";

    printf("%d\n", strcspn(str, " "));

    printf("%d\n", strcspn(str, "/-"));

    printf("%d\n", strcspn(str, "1234567890"));

}


執行結果:

5 //只計算到" "的出現, 所以返回"Linux"的長度

33 //計算到出現"/"或"-", 所以返回到"6"的長度

30 // 計算到出現數字字符為止, 所以返回"3"出現前的長度

參考資料