乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      字符串分割

       昵稱3256870 2010-09-19

      strsep

        原型:char *strsep(char **stringp, const char *delim);
        功能:分解字符串為一組字符串。
        示例:    
        #include <stdio.h>
        #include <string.h>
        int main(void)
        {
        char str[] = "root:x::0:root:/root:/bin/bash:";
        char *buf;
        char *token;
        buf = str;
        while((token = strsep(&buf, ":")) != NULL){
        printf("%s\n", token);
        }  
        return 0;
        }
       
       
       
       

      原型

        char *strtok(char *s, char *delim);

      功能

        分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。

      說明

        首次調(diào)用時,s指向要分解的字符串,之后再次調(diào)用要把s設(shè)成NULL。
        strtok在s中查找包含在delim中的字符并用NULL('')來替換,直到找遍整個字符串。
        char * p = strtok(s,";");
        p = strtok(null,";");
        在調(diào)用的過程中,字串s被改變了,這點是要注意的。

      返回值

        從s開頭開始的一個個被分割的串。當(dāng)沒有被分割的串時則返回NULL。
        所有delim中包含的字符都會被濾掉,并將被濾掉的地方設(shè)為一處分割的節(jié)點。

      strtok函數(shù)在C和C++語言中的使用

        strtok函數(shù)會破壞被分解字符串的完整,調(diào)用前和調(diào)用后的s已經(jīng)不一樣了。如果
        要保持原字符串的完整,可以使用strchr和sscanf的組合等。

      c

        #include <string.h>
        #include <stdio.h>
        int main(void)
        {
        char input[16] = "abc,d";
        char *p;
        /**/ /* strtok places a NULL terminator
        in front of the token, if found */
        p = strtok(input, ",");
        if (p) printf("%s\n", p);
        /**/ /* A second call to strtok using a NULL
        as the first parameter returns a pointer
        to the character following the token */
        p = strtok(NULL, ",");
        if (p) printf("%s\n", p);
        return 0;
        }

      c++

        #include <iostream>
        #include <cstring>
        using namespace std;
        int main()
        {
        char sentence[]="This is a sentence with 7 tokens";
        cout<<"The string to be tokenized is:\n"<<sentence<<"\n\nThe tokens are:\n\n";
        char *tokenPtr=strtok(sentence," ");
        while(tokenPtr!=NULL)
        {
        cout<<tokenPtr<<'\n';
        tokenPtr=strtok(NULL," ");
        }
        cout<<"After strtok, sentence = "<<sentence<<endl;
        return 0;
        }

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多