字符串处理分割字符串
分解字符串為一組字符串。s為要分解的字符,delim為分隔符字符(如果傳入字符串,則傳入的字符串中每個字符均為分割符)。首次調用時,s指向要分解的字符串,之后再次調用要把s設成NULL。
**分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。
例如:strtok(“abc,def,ghi”,”,”),最后可以分割成為abc def ghi.尤其在點分十進制的IP中提取應用較多。
strtok的函數原型為char strtok(char *s, char *delim),功能為“Parse S into tokens separated by characters in DELIM.If S is NULL, the saved pointer in SAVE_PTR is used as the next starting point. ” 翻譯成漢語就是:作用于字符串s,以包含在delim中的字符為分界符,將s切分成一個個子串;如果,s為空值NULL,則函數保存的指針SAVE_PTR在下一次調用中將作為起始位置。*
strtok()用來將字符串分割成一個個片段。參數s指向欲分割的字符串,參數delim則為分割字符串中包含的所有字符。當strtok()在參數s的字符串中發現參數delim中包含的分割字符時,則會將該字符改為\0 字符。在第一次調用時,strtok()必需給予參數s字符串,往后的調用則將參數s設置成NULL。每次調用成功則返回指向被分割出片段的指針。
例子:
輸出:
/home/andrew/文檔/IMPORTENT_C/cmake-build-debug/IMPORTENT_C abc dProcess finished with exit code 0C++例子:
#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=" << tokenPtr<<endl;return 0; }strsep()用于分解字符串為一組字符串。
事例:
#include <stdio.h> #include <memory.h>int main(){int len, nel;char query[] ="user_command=appleboy&test=1&test2=2";char *q, *name, *value;/* Parse into individualassignments */q = query;fprintf(stderr, "CGI[query string] : %s\n",query);len = strlen(query);nel = 1;while (strsep(&q, "&"))nel++;fprintf(stderr, "CGI[nel string] : %d\n", nel);for (q = query; q< (query + len);) {value = name = q;/* Skip to next assignment */fprintf(stderr, "CGI[string] :%s\n", q);fprintf(stderr, "CGI[stringlen] : %d\n", strlen(q));fprintf(stderr, "CGI[address] :%p\n", q);for (q += strlen(q); q < (query +len) && !*q; q++);/* Assign variable */name = strsep(&value,"=");fprintf(stderr, "CGI[name ] :%s\n", name);fprintf(stderr, "CGI[value] :%s\n", value);}return 0;} /home/andrew/文檔/IMPORTENT_C/cmake-build-debug/IMPORTENT_C CGI[query string] : user_command=appleboy&test=1&test2=2 CGI[nel string] : 4 CGI[string] :user_command=appleboy CGI[stringlen] : 21 CGI[address] :0x7ffe5cf043b0 CGI[name ] :user_command CGI[value] :appleboy CGI[string] :test=1 CGI[stringlen] : 6 CGI[address] :0x7ffe5cf043c6 CGI[name ] :test CGI[value] :1 CGI[string] :test2=2 CGI[stringlen] : 7 CGI[address] :0x7ffe5cf043cd CGI[name ] :test2 CGI[value] :2Process finished with exit code 0數據處理,函數格式化輸出打印之后的數據處理之—–strtod()函數
strtod()函數
輸出結果:
/home/andrew/文檔/IMPORTENT_C/cmake-build-debug/IMPORTENT_C a=12345.678900 b=1234.567000 endptr=qwer c=-2322300.000000Process finished with exit code 0 char buf[512] = ; sscanf("123456 ", "%s", buf); printf("%s\n", buf); 結果為:1234562. 取指定長度的字符串。如在下例中,取最大長度為4字節的字符串。sscanf("123456 ", "%4s", buf); printf("%s\n", buf); 結果為:12343. 取到指定字符為止的字符串。如在下例中,取遇到空格為止字符串。 sscanf("123456 abcdedf", "%[^ ]", buf); printf("%s\n", buf); 結果為:1234564. 取僅包含指定字符集的字符串。如在下例中,取僅包含1到9和小寫字母的字符串。sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf); printf("%s\n", buf); 結果為:123456abcdedf5. 取到指定字符集為止的字符串。如在下例中,取遇到大寫字母為止的字符串。 sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf); printf("%s\n", buf); 結果為:123456abcdedf6、給定一個字符串iios/12DDWDFF@122,獲取 / 和 @ 之間的字符串,先將 "iios/"過濾掉,再將非'@'的一串內容送到buf中 sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf); printf("%s\n", buf); 結果為:12DDWDFF7、給定一個字符串"hello, world",僅保留"world"。(注意:“,”之后有一空格) sscanf(“hello, world”, "%*s%s", buf); printf("%s\n", buf); 結果為:worldP.S. %*s表示第一個匹配到的%s被過濾掉,即hello被過濾了,如果沒有空格則結果為NULL。[2]//Compiled with Visual Studio 2015. //================Original File===================*/ // crt_sscanf.c // compile with: /W3 // This program uses sscanf to read data items // from a string named tokenstring, then displays them. # include <stdio.h> int main( void ) {char tokenstring[] = "15 12 14...";char s[81];char c;int i;float fp;// Input various data from tokenstring:// max 80 character string:sscanf( tokenstring, "%80s", s ); // C4996sscanf( tokenstring, "%c", &c ); // C4996sscanf( tokenstring, "%d", &i ); // C4996sscanf( tokenstring, "%f", &fp ); // C4996// Note: sscanf is deprecated; consider using sscanf_s instead // Output the data readprintf( "String = %s\n", s );printf( "Character = %c\n", c );printf( "Integer: = %d\n", i );printf( "Real: = %f\n", fp );}
輸出:
%[a-z] 表示匹配a到z中任意字符,貪婪性(盡可能多的匹配) %[aB'] 匹配a、B、'中一員,貪婪性 %[^a] 匹配非a的任意字符,并且停止讀入,貪婪性
總結
以上是生活随笔為你收集整理的字符串处理分割字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上元之夜丨《大数据》与您共度佳节!
- 下一篇: 【2017年第4期】专题:大数据标准