首页 > 代码库 > C++ string功能补充(类型互转,分割,合并,瘦身)
C++ string功能补充(类型互转,分割,合并,瘦身)
int StringUtil::intFromString(string data)
{
//NOTE atoi是非标准C函数
return atoi(data.c_str());
}
string StringUtil::stringFromInt(int data)
{
char tmp[11];
memset(tmp,0,11);
sprintf(tmp,"%10d",data);
return string(tmp);
}
double StringUtil::doubleFromString(string data)
{
double tmp;
sscanf(data.c_str(),"%lf",&tmp);
return tmp;
}
string StringUtil::stringFromDouble(double data)
{
char tmp[21];
memset(tmp,0,21);
sprintf(tmp,"%20.3lf",data);
return string(tmp);
}
float StringUtil::floatFromString(std::string data)
{
float tmp;
sscanf(data.c_str(),"%f",&tmp);
return tmp;
}
std::string StringUtil::stringFromFloat(float data)
{
char tmp[21];
memset(tmp,0,21);
sprintf(tmp,"%20.3f",data);
return string(tmp);
}
bool StringUtil::boolFromString(std::string data)
{
if(data.compare("true") == 0)
return true;
else
return false;
}
std::string StringUtil::stringFromBool(bool data)
{
if(data)
return string("true");
else
return string("false");
}
vector<std::string> StringUtil::splitStringToArray(std::string source, std::string seperator)
{
vector<string> result;
if(!source.empty())
{
string::size_type begin = 0;
string::size_type end = 0;
unsigned int sepSize = seperator.size();
while((end = source.find_first_of(seperator,begin))!=string::npos)
{
string item = source.substr(begin,end-begin);
result.push_back(item);
begin=end + sepSize;
}
//last item,注意如果最后是分割符,则最后的元素为空字符串
if(begin <= source.size())
{
string item = source.substr(begin,source.size() - begin);
result.push_back(item);
}
}
return result;
}
std::string StringUtil::linkArrayToString(vector<std::string> array, std::string seperator)
{
string result;
if(array.size() > 0)
{
unsigned int limit = array.size() - 1;
for(unsigned int i=0;i< limit;++i)
{
result+=array[i];
result+=seperator;
}
result += array[limit];
}
return result;
}
map<std::string, std::string> StringUtil::splitStringToMap(std::string source, std::string primarySep, std::string secondarySep)
{
vector<string> array = StringUtil::splitStringToArray(source,primarySep);
vector<string> tmpArray;
map<string,string> result;
for(unsigned int i = 0; i< array.size();++i)
{
tmpArray = StringUtil::splitStringToArray(array[i],secondarySep);
if(tmpArray.size() >= 2)
{
result[tmpArray[0]] = tmpArray[1];
}
}
return result;
}
std::string StringUtil::linkMapToString(map<std::string, std::string> tmpMap, std::string primarySep, std::string secondarySep)
{
vector<string> tmpArray;
string tmpStr;
map<string,string>::iterator it = tmpMap.begin();
for(;it!=tmpMap.end();++it)
{
tmpStr = it->first+secondarySep+it->second;
tmpArray.push_back(tmpStr);
}
return StringUtil::linkArrayToString(tmpArray,primarySep);
}
std::string StringUtil::trimFront(std::string data)
{
unsigned int i = 0;
for(;i<data.size()&&data.at(i)==‘ ‘;++i)
{
}
if(i<data.size())
return data.substr(i,data.size()-i);
else
return string("");
}
std::string StringUtil::trimBack(std::string data)
{
int i = data.size()-1;
for(;i>=0&&data.at(i)==‘ ‘;--i)
{
}
if(i>=0)
return data.substr(0,i+1);
else
return string("");
}
std::string StringUtil::trim(std::string data)
{
string tmp = StringUtil::trimFront(data);
return StringUtil::trimBack(tmp);
}
C++ string功能补充(类型互转,分割,合并,瘦身)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。