C++之dolersplit和dolerl

阅读: 评论:0

C++之dolersplit和dolerl

C++之dolersplit和dolerl

在很多情况要把字符串按指定字符串分割得到分割的字符串数组或者长度。C#时候的split方法都是支持按字符分割的,我每次都是把字符串替换为不可见字符再split的。这次参照M和C#包装dolersplit和dolerl方法。来把字符串分割成向量,和取字符串按特定字符分割的长度。

头文件

#pragma onece
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;///用C++实现M常用操作字符串方法$改doler///按指定字符串分割字符串取指定位置
///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:源串
///split:分割串
///index:位置,按M习惯从1开始
void dolerp(string &retStr,string source,string split,int index)
{retStr="";//源串为空返回空if(source==""){return;}//分割串为空返回空if(split==""){return;}//位置不合格返回空if(index<1){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){curIndex++;if(curIndex==index){endIndex=i-spLen;break;}if(curIndex==index-1){startIndex=i+1;}i+=spLen;}}}//不包含子串返回源串if(curIndex==0&&index==1){retStr=source;return;}//没有取的位数返回空else if(curIndex<index-1){return;}else{if(startIndex>endIndex){endIndex=sourceLen-1;}int retArrLen=endIndex-startIndex+1;char retArr[retArrLen];for(int k=startIndex;k<=endIndex;k++){retStr+=source[k];}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));i+=spLen;startIndex=i;}}}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;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;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]$ cmake /zlz/
-- Configuring done
-- Generating done
-- Build files have been written to: /zlz/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
测试分割得到长度*********************
[zhanglianzhu@zlzlinux mutil]$ 

至此包装3个C++字符串操作方法,一个个慢慢包工具方法

本文发布于:2024-02-02 00:34:28,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170681068840239.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:程不识传笔记
标签:dolersplit   dolerl
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23