字符串处理分割字符串
来源:互联网 发布:mac网游加速器 编辑:程序博客网 时间:2024/05/21 05:20
**strtok**
分解字符串为一组字符串。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。每次调用成功则返回指向被分割出片段的指针。
例子:
#include<string.h>#include<stdio.h>int main(void){ char input[50]="abc,d,asd,asdf,asd,asd,t"; char*p; /*strtok places a NULL terminator infront of the token,if found*/ p=strtok(input,","); if(p) printf("%s\n",p); /*Asecond 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;}
输出:
/home/andrew/文档/IMPORTENT_C/cmake-build-debug/IMPORTENT_CabcdProcess finished with exit code 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=" << 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_CCGI[query string] : user_command=appleboy&test=1&test2=2CGI[nel string] : 4CGI[string] :user_command=appleboyCGI[stringlen] : 21CGI[address] :0x7ffe5cf043b0CGI[name ] :user_commandCGI[value] :appleboyCGI[string] :test=1CGI[stringlen] : 6CGI[address] :0x7ffe5cf043c6CGI[name ] :testCGI[value] :1CGI[string] :test2=2CGI[stringlen] : 7CGI[address] :0x7ffe5cf043cdCGI[name ] :test2CGI[value] :2Process finished with exit code 0
数据处理,函数格式化输出打印之后的数据处理之—–strtod()函数
strtod()函数
#include<stdlib.h>#include<stdio.h>void main(){ char *endptr; char a[] = "12345.6789"; char b[] = "1234.567qwer"; char c[] = "-232.23e4"; printf( "a=%lf\n", strtod(a,NULL) ); printf( "b=%lf\n", strtod(b,&endptr) ); printf( "endptr=%s\n", endptr ); printf( "c=%lf\n", strtod(c,NULL) );}
输出结果:
/home/andrew/文档/IMPORTENT_C/cmake-build-debug/IMPORTENT_Ca=12345.678900b=1234.567000endptr=qwerc=-2322300.000000Process finished with exit code 0
char buf[512] = ;sscanf("123456 ", "%s", buf);printf("%s\n", buf);结果为:123456 2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。sscanf("123456 ", "%4s", buf);printf("%s\n", buf);结果为:1234 3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。sscanf("123456 abcdedf", "%[^ ]", buf);printf("%s\n", buf);结果为:123456 4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);printf("%s\n", buf);结果为:123456abcdedf 5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。sscanf("123456abcdedfBCDEF", "%[^A-Z]", buf);printf("%s\n", buf);结果为:123456abcdedf 6、给定一个字符串iios/12DDWDFF@122,获取 / 和 @ 之间的字符串,先将 "iios/"过滤掉,再将非'@'的一串内容送到buf中sscanf("iios/12DDWDFF@122", "%*[^/]/%[^@]", buf);printf("%s\n", buf);结果为:12DDWDFF 7、给定一个字符串"hello, world",仅保留"world"。(注意:“,”之后有一空格)sscanf(“hello, world”, "%*s%s", buf);printf("%s\n", buf);结果为:world P.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 ); // C4996 sscanf( tokenstring, "%c", &c ); // C4996 sscanf( tokenstring, "%d", &i ); // C4996 sscanf( tokenstring, "%f", &fp ); // C4996 // Note: sscanf is deprecated; consider using sscanf_s instead // Output the data read printf( "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的任意字符,并且停止读入,贪婪性
- 字符串处理分割字符串
- DELPHI 字符串分割处理
- 字符串分割~~异常处理
- 字符串的处理:分割
- DELPHI 字符串分割处理
- Oracle 字符串分割的处理
- BASH字符串处理:字符串分割为数组
- 字符串处理 explode--使用一个字符串分割另一个字符串
- 分割字符串
- 分割字符串
- 分割字符串
- 分割字符串
- 分割字符串
- 字符串分割
- 字符串分割
- 分割字符串
- 字符串分割
- 字符串分割
- 打开TensorBoard面板(MNIST)
- S5PV210-iROM-ApplicationNote-Preliminary的分析
- Linux-4.9.2内核在mini2440上的移植(十)——ADC驱动移植
- 数据结构:用JS实现栈
- 假的生命游戏
- 字符串处理分割字符串
- 内存中常见异常值的解释(比如0xcccccccc、0xcdcdcdcd和 0xfeeefeee 异常值)以及关于VC 中 debug和Release模式下的变量初始化问题
- C++ typeid操作符
- GUN和GPL的大概意思
- GROMACS运行参数之nvt.mdp文件详解
- 第四周项目三__单链表的应用(2)
- 注重医院患者院后随访提高医疗质量
- DDOS攻击分类
- Sed行定位的使用