急C++中,字符串的操作
急在线等!C++中,字符串的操作
字符串string=“[001172]专题讲座 2.0 41 [0001206]石岩”
如何提取出字符串中的数字?就是分别提取出2.0和41
------解决方案--------------------
楼主,你要描述的清晰一些,2.0 41,这个字符串会变吗?
如果只是这个里面得2.041,
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
------解决方案--------------------
字符串string=“[001172]专题讲座 2.0 41 [0001206]石岩”
如何提取出字符串中的数字?就是分别提取出2.0和41
------解决方案--------------------
楼主,你要描述的清晰一些,2.0 41,这个字符串会变吗?
如果只是这个里面得2.041,
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串
------解决方案--------------------
- C/C++ code
[User:root Time:09:48:38 Path:/home/liangdong/c]$ make gcc -g -I./include -c -o src/main.o src/main.c gcc -o output src/main.o -lpthread -lm -lz Makefile done. [User:root Time:09:48:38 Path:/home/liangdong/c]$ ./output 2.0 41 [User:root Time:09:48:41 Path:/home/liangdong/c]$ cat src/main.c #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* const argv[]) { const char *str = "[001172]专题讲座 2.0 41 [0001206]石岩"; char version[10]; char number[10]; int ret = sscanf(str, "%*s%9s%9s%*s", version, number); //铭记安全编程 if (ret == 2) { printf("%s\n%s\n", version, number); } return 0; }