常用函数-String

/************************************************************************
函数功能:将字符串中str的old_value子字符串,替换为new_valud字符串
输入参数:string&   str             -- 要修改的字符串
const string& old_value   -- 要被替换的子字符串
const string& new_value   -- 要插入的字符串
输出参数:
返回值  : 返回修改后的字符串
************************************************************************/
string& replace_all(string& str, const string& old_substr, const string& new_substr)
{
    try
    {
        for (string::size_type pos(0); pos != string::npos; pos += new_substr.length())
        {
            if ((pos = str.find(old_substr, pos)) != string::npos)
                str.replace(pos, old_substr.length(), new_substr);
            else
                break;
        }
    }
    catch (...)
    {
    }
    return   str;
}
/************************************************************************
函数功能: 计算子字符串substr在字符串str中出现的次数
输入参数: const string& str    -- 字符串对象
输出参数: const string& substr -- 要计算其出现次数的子字符串对象
返回值  : 整型值,子字符串对象的出现次数
说明    :
************************************************************************/
int find_num_of_substr(const string& str, const string& substr)
{
    int num = 0;
    string::size_type pos = 0;
    string::size_type loc = 0;
    while(pos != string::npos)
    {
        loc = str.find(substr, pos);
        if(loc != string::npos)
        {
            num++;
            pos = loc;
            pos += substr.length();
        }
        else
        {
            pos = loc;
        }
    }
    return num;
};
/************************************************************************
函数功能: 对于输入的字符串对象,删除其前后的空格,制表符
输入参数: string& s  --  要删除前后空格,制表符的字符串对象
输出参数: 
返回值  : 
说明    :
************************************************************************/
void DPC::dcs_trim(string& s)
{
    int len = s.length();
    if(len == 0)
        return;
int pos = -1; // trim left for(int i = 0; i < len; ++i) { if(s[i] == ' ' || s[i] == ' ') pos = i; else break; } if(pos != -1) s.erase(0, pos - 0 + 1); len = s.length(); if(len == 0) return; pos = - 1; // trim right for(int j = len - 1; j >= 0; --j) { if(s[j] == ' ' || s[j] == ' ') pos = j; else break; } if(pos != -1) s.erase(pos); };
/************************************************************************
函数功能:将字符串中str的old_value子字符串,替换为new_valud字符串
输入参数:string&   str             -- 要修改的字符串
const string& old_value   -- 要被替换的子字符串
const string& new_value   -- 要插入的字符串
输出参数:
返回值  : 返回修改后的字符串
************************************************************************/
string& DPC::replace_all(string& str, const string& old_substr, const string& new_substr)
{
    try
    {
        for (string::size_type pos(0); pos != string::npos; pos += new_substr.length())
        {
            if ((pos = str.find(old_substr, pos)) != string::npos)
                str.replace(pos, old_substr.length(), new_substr);
            else
                break;
        }
    }
    catch (...)
    {
    }
    return   str;
}
原文地址:https://www.cnblogs.com/osbreak/p/10061408.html