字符串常用操作有分割取子串,取分割长度,分割得到数组,替换,去除两边空格,查找等。本次实现替换和剔除空格方法。
头文件
#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
# : 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]$
抽空逐渐把这个包装成好用的函数库。
本文发布于:2024-02-02 00:34:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170681067840238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |