c++ 字符串分割一、用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串。 参数说明:
2013-10-28 11:26
821 查看
一、用strtok函数进行字符串分割
原型: char *strtok(char *str, const char *delim);
功能:分解字符串为一组字符串。
参数说明:str为要分解的字符串,delim为分隔符字符串。
返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。
二、用STL进行字符串的分割
涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos
2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串
原型: char *strtok(char *str, const char *delim);
功能:分解字符串为一组字符串。
参数说明:str为要分解的字符串,delim为分隔符字符串。
返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。
#include <string.h> #include <stdio.h> int main() { char s[] = "Golden Global View,disk * desk"; const char *d = " ,*"; char *p; p = strtok(s,d); while(p) { printf("%s\n",p); p=strtok(NULL,d); } return 0; }
二、用STL进行字符串的分割
涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos
2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
std::vector<std::string> split(std::string str,std::string pattern) { std::string::size_type pos; std::vector<std::string> result; str+=pattern;//扩展字符串以方便操作 int size=str.size(); for(int i=0; i<size; i++) { pos=str.find(pattern,i); if(pos<size) { std::string s=str.substr(i,pos-i); result.push_back(s); i=pos+pattern.size()-1; } } return result; }
返回值:子字符串
相关文章推荐
- 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C
- 【C语言】为下面的函数原型编写函数定义: int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
- C/C++如何把一个特定分隔符分割的字符串划分为一个字符串数组,strtok(char *,char*)分割函数
- 编写一个函数 int count_chars(char const *str,char const *chars) 函数应该在第一个参数中进行查找, 并返回匹配第二个参数所包含的字符的数量。
- 函数在一个字符串中进行搜索,查找所有在一个给定字符集合中出现的字符,函数原型:char *find_char( char const *source, char const *chars);
- [转载] 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc),编写函数 strcpy(C++版)
- 为下面的函数原型编写函数定义: int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。如果字符串参数
- 【C++】字符串分割 strtok_s
- C++: VC6转VC8出现问题:error C2664: “fopen”: 不能将参数 1 从“CString”转换为“const char *”
- 【初级】String str=" ac,42,123,sd Fa,c df,4,acdf,5ewRRre ";1.把字符串按,进行分割
- C实现分割字符串(使用strtok_s函数,完成C#的Split函数功能)
- 编写一个函数,将一个数字字符串转换成该字符串对应的数字(包括正整数、负整数) 例如:“12“ 返回12 “-123“ 返回-123 函数原型:int my_atoi(char *str) {}
- Java调用C++(包含jstring到const char*字符串转化) 及其c++调用java字符串传递
- c++分割字符串,strtok与strtok_r的使用记录
- C++字符串分割strtok
- int ascii_to_integer(char *str); 这个字符串参数必须包含一个或者多个数字,函数应该把这些数字字符转换为整数并返回这个整数。
- 【C编程练习】华为2013校园招聘机试题3:对源字符串中得字符串进行查找替换并返回动态申请空间的指针char* Fun(char*pSrc,char*pOldStr, char*pNewStr)
- 笔试题目2 字符串拷贝函数 char *strcpy(char *strDst,const char *strSrc)
- 编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割
- 算法题 用C或C++实现void reverse(char* str)函数,即反转一个null结尾的字符串