如何精简程序,让程序更美观?

如题,我编写了一个查找\,,\:,\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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值