/************************************************************************
函数功能:将字符串中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(); 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 = ;
string::size_type pos = ;
string::size_type loc = ;
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 == )
return;
int pos = -; // trim left
for(int i = ; i < len; ++i)
{
if(s[i] == ' ' || s[i] == '\t')
pos = i;
else
break;
}
if(pos != -)
s.erase(, pos - + ); len = s.length();
if(len == )
return;
pos = - ;
// trim right
for(int j = len - ; j >= ; --j)
{
if(s[j] == ' ' || s[j] == '\t')
pos = j;
else
break;
}
if(pos != -)
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(); 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;
}