• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            strtok

            Posted on 2010-09-15 13:47 Prayer 閱讀(712) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            strtok

            目錄

            原型
            功能
            說明
            返回值
            strtok函數在C和C++語言中的使用
            1. c
            2. c++
            其他相關信息
            展開

            原型

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

            功能

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

            說明

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

            返回值

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

            strtok函數在C和C++語言中的使用

              strtok函數會破壞被分解字符串的完整,調用前和調用后的s已經不一樣了。如果
              要保持原字符串的完整,可以使用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;
              }
              函數第一次調用需設置兩個參數。第一次分割的結果,返回串中第一個 ',' 之前的字符串,也就是上面的程序第一次輸出abc。
              第二次調用該函數strtok(NULL,","),第一個參數設置為NULL。結果返回分割依據后面的字串,即第二次輸出d。
              strtok是一個線程不安全的函數,因為它使用了靜態分配的空間來存儲被分割的字符串位置
              線程安全的函數叫strtok_r,ca
              運用strtok來判斷ip或者mac的時候務必要先用其他的方法判斷'.'或':'的個數,因為用strtok截斷的話,比如:"192..168.0...8..."這個字符串,strtok只會截取四次,中間的...無論多少都會被當作一個key

            其他相關信息

              下面的說明摘自于最新的Linux內核2.6.29,說明了這個函數已經不再使用,由速度更快的strsep()代替
              /*
              * linux/lib/string.c
              *
              * Copyright (C) 1991, 1992 Linus Torvalds
              */
              /*
              * stupid library routines.. The optimized versions should generally be found
              * as inline code in <asm-xx/string.h>
              *
              * These are buggy as well..
              *
              * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
              * - Added strsep() which will replace strtok() soon (because strsep() is
              * reentrant and should be faster). Use only strsep() in new code, please.
              *
              * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
              * Matthew Hawkins <matt@mh.dropbear.id.au>
              * - Kissed strtok() goodbye
            精品久久久无码21p发布| 久久久精品国产亚洲成人满18免费网站 | 久久久久久A亚洲欧洲AV冫| 久久激情亚洲精品无码?V| 亚洲а∨天堂久久精品9966| 久久久噜噜噜久久中文字幕色伊伊| 影音先锋女人AV鲁色资源网久久| 久久亚洲欧美日本精品| 无码国内精品久久人妻麻豆按摩| 久久久无码一区二区三区| 国内精品伊人久久久久网站| 亚洲第一极品精品无码久久| 91精品国产综合久久四虎久久无码一级| 人人狠狠综合久久亚洲高清| 久久丫精品国产亚洲av不卡| 99久久精品免费看国产一区二区三区| 一级a性色生活片久久无少妇一级婬片免费放 | 亚洲欧洲精品成人久久曰影片| 久久99精品国产自在现线小黄鸭 | 少妇熟女久久综合网色欲| 久久99国产精品久久久 | 久久99热这里只频精品6| 久久精品成人免费网站| 波多野结衣AV无码久久一区| 国内精品久久久久久麻豆| 国产精品女同久久久久电影院 | 国内精品免费久久影院| 2022年国产精品久久久久| 亚洲va久久久噜噜噜久久狠狠 | 久久久精品人妻一区二区三区蜜桃| 国产午夜久久影院| 狠狠色婷婷久久一区二区三区| 久久久久亚洲AV片无码下载蜜桃| 久久久精品国产亚洲成人满18免费网站| 国产成人久久激情91| 久久大香香蕉国产| 精品永久久福利一区二区| 久久人人爽人人爽人人AV东京热| 天堂久久天堂AV色综合| 亚洲va久久久噜噜噜久久| 久久久噜噜噜久久中文福利|