码迷,mamicode.com
首页 > 编程语言 > 详细

C++ string功能补充(类型互转,分割,合并,瘦身)

时间:2014-11-21 18:18:22      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:ar   os   sp   for   on   bs   amp   ef   as   

 

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功能补充(类型互转,分割,合并,瘦身)

标签:ar   os   sp   for   on   bs   amp   ef   as   

原文地址:http://www.cnblogs.com/guoxiaoqian/p/4113339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!