C++字符串操作:分割、合并、转数字
1.字符串分割
- 按输入字符规则进行分割,以单个字符为规则对字符串进行分割
//字符串分割
std::vector<std::string> str_split(const std::string& str, const char split)
{
std::vector<std::string>res;
if (str == "") return res;
//在字符串末尾也加入分隔符,方便截取最后一段
std::string strs = str + split;
size_t pos = strs.find(split);
// 若找不到内容则字符串搜索函数返回 npos
while (pos != strs.npos)
{
std::string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(split);
}
return res;
}
若要实现以多个特殊字符[',','.','-',' ']
为分割准则,可以循环使用上述函数,或者参考下边方法:
- 以多个字符为标准进行字符串分割:
参考:C++ 将string字符串按照特殊的多个字符分割
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
vector<string> split(string strTime)
{
vector<string> result;
string temp("");
strTime+='-';//字符串结束标记,方便将最后一个单词入vector
for(size_t i = 0; i < strTime.size(); i++)
{
if(strTime[i] == '-' || strTime[i] == '.' || strTime[i] == '\t' ||strTime[i] == ':')
{
result.push_back(temp);
temp = "";
}
else
{
temp += strTime[i];
}
}
return result;
}
int main()
{
string strtime="2014-11-10 14:55:40.123";
vector<string> result = split(strtime);
for(size_t i = 0; i < result.size(); i++)
{
cout<<result[i]<<" ";
}
return 0;
}
输出:
开始运行...
2014 11 10 14 55 40 123
运行结束。
或者方法二:
参考:C++ 以多字符为分隔符分割字符串
#include <iostream>
#include <vector>
#include <string>
using namespace std;
char *strsep(char **stringp, const char *delim)
{
//判断stringp是否为空,提高程序的容错性
char *s;
const char *spanp;
int c, sc;
char *tok;
if ((s = *stringp)== NULL)
return (NULL);
for (tok = s;;) {
c = *s++;
spanp = delim;
do {
if ((sc =*spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
}
void pushToVec(std::vector<std::string>&obj,const char *param,std::string token)
{
char *p = (char*)param;
char *key_point;
while(p)
{
while ( key_point = strsep(&p,token.c_str()))//关键字为c或d,它们连续出现了
{
//函数遇到token时,key_point指向token返回
if (*key_point == 0)
continue;
else
break;
}
if (key_point)
obj.push_back(key_point);
}
}
int main()
{
vector<string> obj;
string test("145,456,789,rtyy,http:www.baidu.com;");
string token(",:.;"); //使用","号,":"号,"."号,";"做分隔符
pushToVec(obj,test.c_str(),token);
//for (size_t i = 0;i< obj.size();i++)
for(auto i:obj)
{
//cout<<obj[i]<<endl;
cout<<i<<endl;
}
return 0;
}
输出:
145
456
789
rtyy
http
wwwbaidu
com
2. 字符串与数字转换
s=str_split(datas_new[i], ':');
std::vector<std::string> _id = str_split(s[1], '\t');
//.c_strz()将字符串转为const型
int id = atoi(_id[0].c_str());
- C++库
string
的使用:
#include <../include/string>
#if _MSC_VER < 1800
namespace std
{
inline string to_string(int _Val)
{
char _Buf[2 * _MAX_INT_DIG];
_CSTD sprintf_s(_Buf, sizeof(_Buf), "%d", _Val);
return (string(_Buf));
}
inline string to_string(unsigned int _Val)
{
char _Buf[2 * _MAX_INT_DIG];
_CSTD sprintf_s(_Buf, sizeof(_Buf), "%u", _Val);
return (string(_Buf));
}
inline string to_string(long _Val)
{
char _Buf[2 * _MAX_INT_DIG];
_CSTD sprintf_s(_Buf, sizeof(_Buf), "%ld", _Val);
return (string(_Buf));
}
inline string to_string(unsigned long _Val)
{
char _Buf[2 * _MAX_INT_DIG];
_CSTD sprintf_s(_Buf, sizeof(_Buf), "%lu", _Val);
return (string(_Buf));
}
}
#endif // _MSC_VER < 1800
#else // _MSC_VER
#include_next <string>
#endif // _MSC_VER
3.string与char* 、char []相互转换
- string转char*,主要有三种方法可以将str转换为char*类型,分别是:
data()
;c_str()
;copy()
;
//data()方法
string str = "hello";
const char* p = str.data();//加const 或者用char * p=(char*)str.data();的形式
//c_str()方法
string str=“world”;
const char *p = str.c_str();//同上,要加const或者等号右边用char*
//copy()方法
string str="hmmm";
char p[50];
str.copy(p, 5, 0);//这里5代表复制几个字符,0代表复制的位置,
*(p+5)=‘\0’;//注意手动加结束符!!!
- char * 转string,可以直接赋值:
string s;
char *p = "hello";//直接赋值
s = p;
- string转char[],知道string的长度,可以根据length()函数得到,又可以根据下标直接访问,所以用一个循环就可以赋值了。
string pp = "dagah";
char p[8];
int i;
for( i=0;i<pp.length();i++)
p[i] = pp[i];
p[i] = '\0';
printf("%s\n",p);
cout<<p;