我编写了一个查找\,,\:,\r,\n的4层嵌套if语句的程序,将字符串中的所有上述4个字符删除,感觉有点绕,能不能帮忙精简一下,程序如下:
#include<string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//功能: 删除字符串中的"\r" "\n""\,""\:"
string StrClean(const string &strSource)
{
string strDes = strSource;
string::size_type index = 0;
do
{
index = strDes.find("\\:");
if (index != string::npos)
{
strDes = strDes.replace(index, strlen("\\:")," ");
}
else
{
index = strDes.find("\\r");
if (index != string::npos)
{
strDes = strDes.replace(index, strlen("\\r")," ");
}
else
{
index = strDes.find("\\n");
if (index != string::npos)
{
strDes = strDes.replace(index, strlen("\\n")," ");
}
else
{
index = strDes.find("\\,");
if (index != string::npos)
{
strDes = strDes.replace(index, strlen("\\,")," ");
}
}
}
}
} while (string::npos != index);
return strDes;
}
int main()
{
string str = "123\:12\,esg\rsf\n if \: will my name\, gs\r\ntf\vs\r\nc",stre;
cout << "the source string is :" << str << endl;
//str.remove("\r\n");
//str.replace("\r\n", "");
stre=StrClean(str);
cout << "the destination string is :" << stre << endl;
return 0;
}
|