C++之$replace和$trim

字符串常用操作有分割取子串,取分割长度,分割得到数组,替换,去除两边空格,查找等。本次实现替换和剔除空格方法。

头文件

#pragma onece
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

///用C++实现M常用操作字符串方法$改doler

///给字符串剔除左边的空格
///retStr:返回串
///source:源串
void dolertrimleft(string &retStr,string source);

///给字符串剔除右边的空格
///retStr:返回串
///source:源串
void dolertrimright(string &retStr,string source);

///给字符串剔除两边的空格
///retStr:返回串
///source:源串
void dolertrim(string &retStr,string source);

///用给定的字符串替换字符串里的指定字符串
///retStr:返回串
///source:源串
///orgStr:要替换的串
///repStr:替换的串
void dolerreplace(string &retStr,string source,string orgStr,string repStr);

///按指定字符串分割字符串取指定位置
///retStr:返回串
///source:源串
///split:分割串
///index:位置,按M习惯从1开始
void dolerp(string &retStr,string source,string split,int index);

///按指定字符串分割字符串返回向量
///retvec:返回向量
///source:源串
///split:分割串
void dolersplit(vector<string> &retvec,string source,string split);

///按指定字符串分割字符串的长度
///source:源串
///split:分割串
int dolerl(string source,string split);

实现cpp

#include "mutil.h"

///给字符串剔除左边的空格
///retStr:返回串
///source:源串
void dolertrimleft(string &retStr,string source)
{
    string retStrTmp="";
    //源串为空返回空
    if(source=="")
    {
        retStr=retStrTmp;
        return;
    }
    int sourceLen=source.length();
    int startIndex=-1;
    for(int i=0;i<sourceLen;i++)
    {
        char one=source[i];
        if(one!=' ')
        {
            startIndex=i;
            break;
        }
    }
    //全部是空格
    if(startIndex==-1)
    {
        retStr=retStrTmp;
        return;
    }
    retStrTmp=source.substr(startIndex,sourceLen-startIndex);
    retStr=retStrTmp;
}

///给字符串剔除右边的空格
///retStr:返回串
///source:源串
void dolertrimright(string &retStr,string source)
{
    string retStrTmp="";
    //源串为空返回空
    if(source=="")
    {
        retStr=retStrTmp;
        return;
    }
    int sourceLen=source.length();
    int endIndex=-1;
    for(int i=sourceLen-1;i>=0;i--)
    {
        char one=source[i];
        if(one!=' ')
        {
            endIndex=i;
            break;
        }
    }
    //全部是空格
    if(endIndex==-1)
    {
        retStr=retStrTmp;
        return;
    }
    retStrTmp=source.substr(0,endIndex+1);
    retStr=retStrTmp;
}

///给字符串剔除两边的空格
///retStr:返回串
///source:源串
void dolertrim(string &retStr,string source)
{
    string tmpstr;
    dolertrimleft(tmpstr,source);
    dolertrimright(retStr,tmpstr);
}

///用给定的字符串替换字符串里的指定字符串
///retStr:返回串
///source:源串
///orgStr:要替换的串
///repStr:替换的串
void dolerreplace(string &retStr,string source,string orgStr,string repStr)
{
    string retStrTmp="";
    //源串为空返回空
    if(source=="")
    {
        retStr=retStrTmp;
        return;
    }
    //要替换的串为空返回空
    if(orgStr=="")
    {
        retStrTmp=source;
        retStr=retStrTmp;
        return;
    }
    int sourceLen=source.length();
    int orgLen=orgStr.length();
    int curIndex=0;
    int startIndex=0;
    //遍历找到按分割串分割的指定位数的串
    for(int i=0;i<sourceLen;i++)
    {
        //当前位置大于等于分割串长度,且当前字符等于分割串最后一位就比前面字符
        if(i>=orgLen&&source[i]==orgStr[orgLen-1])
        {
            //时候和分割串相同
            bool isCommon=true;
            for(int j=1;j<orgLen;j++)
            {
                if(source[i-j]!=orgStr[orgLen-1-j])
                {
                    isCommon=false;
                    break;
                }
            }
            if(isCommon==true)
            {
                curIndex++;
                int len=i-orgLen-startIndex+1;
                if(curIndex>1)
                {
                    retStrTmp+=repStr+source.substr(startIndex,len);
                }
                else
                {
                    retStrTmp=source.substr(startIndex,len);
                }
                startIndex=i+1;
                i+=orgLen;
            }
            
        }
    }
    if(startIndex<sourceLen)
    {
        if(curIndex>1)
        {
            retStrTmp+=repStr+source.substr(startIndex,sourceLen-startIndex);
        }
        else
        {
            retStrTmp=source.substr(startIndex,sourceLen-startIndex);
        }
        
    }
    retStr=retStrTmp;
}

///按指定字符串分割字符串取指定位置
///retStr:返回串
///source:源串
///split:分割串
///index:位置,按M习惯从1开始
void dolerp(string &retStr,string source,string split,int index)
{
    string retStrTmp="";
    //源串为空返回空
    if(source=="")
    {
        retStr=retStrTmp;
        return;
    }
    //分割串为空返回空
    if(split=="")
    {
        retStr=retStrTmp;
        return;
    }
    //位置不合格返回空
    if(index<1)
    {
        retStr=retStrTmp;
        return;
    }
    int sourceLen=source.length();
    //源串为空返回空
    if(sourceLen==0)
    {
        retStr=retStrTmp;
        return;
    }
    int spLen=split.length();
    //分割串为空返回空
    if(spLen==0)
    {
        retStr=retStrTmp;
        return;
    }
    int curIndex=0;
    int startIndex=0;
    int endIndex=0;
    //遍历找到按分割串分割的指定位数的串
    for(int i=0;i<sourceLen;i++)
    {
        //当前位置大于等于分割串长度,且当前字符等于分割串最后一位就比前面字符
        if(i>=spLen&&source[i]==split[spLen-1])
        {
            //时候和分割串相同
            bool isCommon=true;
            for(int j=1;j<spLen;j++)
            {
                if(source[i-j]!=split[spLen-1-j])
                {
                    isCommon=false;
                    break;
                }
            }
            if(isCommon==true)
            {
                curIndex++;
                if(curIndex==index)
                {
                    endIndex=i-spLen;
                    break;
                }
                if(curIndex==index-1)
                {
                    startIndex=i+1;
                }
                i+=spLen;
            }
            
        }
    }
    //不包含子串返回源串
    if(curIndex==0&&index==1)
    {
        retStrTmp=source;
        retStr=retStrTmp;
        return;
    }
    //没有取的位数返回空
    else if(curIndex<index-1)
    {
        retStr=retStrTmp;
        return;
    }
    else
    {
        if(startIndex>endIndex)
        {
            endIndex=sourceLen-1;
        }
        int retArrLen=endIndex-startIndex+1;
        char retArr[retArrLen];
        for(int k=startIndex;k<=endIndex;k++)
        {
            retStrTmp+=source[k];
        }
        retStr=retStrTmp;
        return;
    }
}

///按指定字符串分割字符串的长度
///source:源串
///split:分割串
int dolerl(string source,string split)
{
    int ret=1;
    //源串为空返回空
    if(source=="")
    {
        return ret;
    }
    //分割串为空返回空
    if(split=="")
    {
        return ret;
    }
    int sourceLen=source.length();
    //源串为空返回空
    if(sourceLen==0)
    {
        return ret;
    }
    int spLen=split.length();
    //分割串为空返回空
    if(spLen==0)
    {
        return ret;
    }
    int curIndex=0;
    int startIndex=0;
    int endIndex=0;
    //遍历找到按分割串分割的指定位数的串
    for(int i=0;i<sourceLen;i++)
    {
        //当前位置大于等于分割串长度,且当前字符等于分割串最后一位就比前面字符
        if(i>=spLen&&source[i]==split[spLen-1])
        {
            //时候和分割串相同
            bool isCommon=true;
            for(int j=1;j<spLen;j++)
            {
                if(source[i-j]!=split[spLen-1-j])
                {
                    isCommon=false;
                    break;
                }
            }
            if(isCommon==true)
            {
                ret++;
                i+=spLen;
            }
            
        }
    }
    return ret;
}

///按指定字符串分割字符串返回向量
///retvec:返回向量
///source:源串
///split:分割串
void dolersplit(vector<string> &retvec,string source,string split)
{
    retvec.clear();
    //源串为空返回空
    if(source=="")
    {
        return;
    }
    //分割串为空返回空
    if(split=="")
    {
        return;
    }
    int sourceLen=source.length();
    //源串为空返回空
    if(sourceLen==0)
    {
        return;
    }
    int spLen=split.length();
    //分割串为空返回空
    if(spLen==0)
    {
        return;
    }
    int curIndex=0;
    int startIndex=0;
    int endIndex=0;
    //遍历找到按分割串分割的指定位数的串
    for(int i=0;i<sourceLen;i++)
    {
        //当前位置大于等于分割串长度,且当前字符等于分割串最后一位就比前面字符
        if(i>=spLen&&source[i]==split[spLen-1])
        {
            //时候和分割串相同
            bool isCommon=true;
            for(int j=1;j<spLen;j++)
            {
                if(source[i-j]!=split[spLen-1-j])
                {
                    isCommon=false;
                    break;
                }
            }
            if(isCommon==true)
            {
                int len=i-spLen-startIndex+1;
                retvec.push_back(source.substr(startIndex,len));
                startIndex=i;
                i+=spLen;
            }
            
        }
    
    }
    if(startIndex<sourceLen)
    {
        retvec.push_back(source.substr(startIndex,sourceLen-startIndex));
    }
}


测试cpp

#include "mutil.h"
///测试
int main(int argc, char* argv[])
{
    string all="张三^29岁^男^433127199101211234";
    string sp="^";
    string ret;
    //测试分割取子串
    cout<<endl;
    cout<<"测试分割取子串*********************"<<endl;
    dolerp(ret,all,sp,1);
    cout<<"第一位为:"<<ret<<endl;
    dolerp(ret,all,sp,2);
    cout<<"第二位为:"<<ret<<endl;
    dolerp(ret,all,sp,3);
    cout<<"第三位为:"<<ret<<endl;
    dolerp(ret,all,sp,4);
    cout<<"第四位为:"<<ret<<endl;
    dolerp(ret,all,"29",2);
    cout<<"29分割第二位为:"<<ret<<endl;
    dolerp(ret,all,"298",2);
    cout<<"298分割第二位为:"<<ret<<endl;
    dolerp(ret,all,"433",1);
    cout<<"433分割第一位为:"<<ret<<endl;


    //测试分割向量
    cout<<endl;
    cout<<"测试分割向量*********************"<<endl;
    vector<string> vecarr;
    dolersplit(vecarr,all,sp);
    for(int i=0;i<vecarr.size();i++)
    {
        cout<<"数组第"<<i<<"位为:"<<vecarr[i]<<endl;
    }

    //测试分割得到长度
    cout<<endl;
    cout<<"测试分割得到长度*********************"<<endl;
    int len=dolerl(all,sp);
    cout<<"长度为:"<<len<<endl;
    for(int i=1;i<=len;i++)
    {
        string childStr;
        dolerp(childStr,all,sp,i);
        cout<<"取第"<<i<<"个字串:"<<childStr<<endl;
    }

    //测试替换字符串
    cout<<endl;
    cout<<"测试替换字符串*********************"<<endl;
    string allrepstr="张三^29岁^男^433127199101211234";
    string repret;
    dolerreplace(repret,allrepstr,"^","$@$");
    cout<<allrepstr<<",替换:^"<<"为:$@$得到:"<<repret<<endl;
    string repret1;
    repret="张三$@$29岁$@$男$@$433127199101211234";
    dolerreplace(repret1,repret,"$@$","%ZSP%");
    cout<<repret<<",替换:$@$"<<"为:%ZSP%得到:"<<repret1<<endl;
    //测试替换字符串

    //测试剔除空格
    cout<<endl;
    cout<<"测试剔除空格*********************"<<endl;
    string hasSpaceStr="   张三 男 29岁  ";
    string trimRet;
    dolertrimleft(trimRet,hasSpaceStr);
    cout<<"去除左空格:"<<trimRet<<endl;
    dolertrimright(trimRet,hasSpaceStr);
    cout<<"去除右空格:"<<trimRet<<endl;
    dolertrim(trimRet,hasSpaceStr);
    cout<<"去除两边空格:"<<trimRet<<endl;
    //测试剔除空格
    return 0;
}

cmake

# CMakeList.txt: mutil 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)

project ("mutil")

# 将源代码添加到此项目的可执行文件。
add_executable (mutil "mutil.cpp" "mutil.h" "main.cpp") 

# TODO: 如有需要,请添加测试并安装目标。

测试效果

[zhanglianzhu@zlzlinux mutil]$ 
[zhanglianzhu@zlzlinux mutil]$ make
Consolidate compiler generated dependencies of target mutil
[ 33%] Building CXX object CMakeFiles/mutil.dir/main.cpp.o
[ 66%] Linking CXX executable mutil
[100%] Built target mutil
[zhanglianzhu@zlzlinux mutil]$ ./mutil

测试分割取子串*********************
第一位为:张三
第二位为:29岁
第三位为:男
第四位为:433127199101211234
29分割第二位为:岁^男^433127199101211234
298分割第二位为:
433分割第一位为:张三^29岁^男^

测试分割向量*********************
数组第0位为:张三
数组第1位为:^29岁
数组第2位为:^男
数组第3位为:^433127199101211234

测试分割得到长度*********************
长度为:4
取第1个字串:张三
取第2个字串:29岁
取第3个字串:男
取第4个字串:433127199101211234

测试替换字符串*********************
张三^29岁^男^433127199101211234,替换:^为:$@$得到:张三$@$29$@$男$@$433127199101211234
张三$@$29$@$男$@$433127199101211234,替换:$@$为:%ZSP%得到:张三%ZSP%29岁%ZSP%男%ZSP%433127199101211234

测试剔除空格*********************
去除左空格:张三 男 29岁  
去除右空格:   张三 男 29岁
去除两边空格:张三 男 29岁
[zhanglianzhu@zlzlinux mutil]$ 

在这里插入图片描述

抽空逐渐把这个包装成好用的函数库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乌鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值