返回顶部

收藏

C++ string split()和 replaceAll()

更多
#include "stdafx.h"
#include <iostream> 
#include <string> 
#include <vector> 

using namespace std; 

vector<string> mySplit(const string&amp; str, string sp_string)  // split(), str 是要分割的string
{ 
    vector<string> vecString; 
    int sp_stringLen = sp_string.size(); 
    int lastPosition = 0; 
    int index = -1; 
    while(-1 != (index=str.find(sp_string , lastPosition))) 
    { 
        vecString.push_back(str.substr(lastPosition, index - lastPosition)); 
        lastPosition = index +sp_stringLen; 
    } 
    string lastStr = str.substr(lastPosition); 
    if ( !lastStr.empty() ) 
    { 
        vecString.push_back(lastStr); 
    } 
    return vecString; 
} 

string myReplaceAll(const string&amp; str, string org_str, string rep_str) // 把org_str 替换为rep_str; 
{ 
    vector<string>  delimVec = mySplit(str, org_str); 
    if (delimVec.size() <= 0) 
    { 
        cout<<"can not find"<<endl; 
        return str; 
    } 
    string target(""); 
    vector<string>::iterator it = delimVec.begin(); 
    for (; it != delimVec.end(); ++it) 
    { 
        target = target + (*it)+ rep_str; 
    } 
//target = target +(*it); 
    return target; 
    } 

// 把org_strVec中的string全部替换为rep_str; 
string myReplaceAll(const string&amp; str, vector<string>&amp; org_strVec, string rep_str)
{ 
    vector<string>::iterator org_it = org_strVec.begin(); 
    string org_str(""); 
    string target = str; 
    string strTarget = str; 
    for (; org_it != org_strVec.end(); ++org_it) 
    { 
        org_str = *org_it; 
        vector<string> delimVec = mySplit(strTarget, org_str); 
        target = ""; 
        if (delimVec.size() > 0) 
        { 
            vector<string>::iterator it = delimVec.begin(); 
            for (;it != delimVec.end(); ++it) 
            { 
                target = target + (*it) +rep_str; 
            } 
            strTarget = target; 
        } 
    } 
    return strTarget; 
} 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    string str = "ewrsdrtwrtsdfsdsdfrtfgsdfertgdffgertsdfsd"; 
    string splitStr = "sd"; 
    string replaceStr1 = "mmmm"; 
    string replaceStr2 = "xxxx"; 
    vector<string> strVec1 = mySplit(str, splitStr); 
    for (vector<string>::iterator it = strVec1.begin(); it != strVec1.end(); ++it) 
    { 
        cout<< "!!!  "<<*it<<endl; 
    } 
    string strX[] = {"wr","rt"}; 
    vector<string> strVec2 (strX, strX + sizeof(strX)/sizeof(strX[0])); 

    cout << strVec2[0]<<"13q3224"<<strVec2[1]<<endl; 
    str = myReplaceAll(str,splitStr,replaceStr1); 
    str = myReplaceAll(str,strVec2,replaceStr2); 
    cout << str <<"~~~~~"<<endl; 
    return 0; 
}

标签:c/c++

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. yuer 发表 2018-07-27 08:46:07 coredump之百米之内必有解药
  2. hev 发表 2018-04-28 06:11:38 一个简单、轻量的 Linux 协程实现
  3. hev 发表 2017-10-19 15:56:11 FSH – 助你接入私有网络中的 Linux 终端
  4. gonwan 发表 2015-04-15 08:03:07 Database Access Layer in C++
  5. gonwan 发表 2015-12-28 08:41:13 Basic Usage of Boost MultiIndex Containers
  6. gonwan 发表 2016-01-19 03:37:54 Coroutines in C++/Boost
  7. Haoxiang Li 发表 2017-10-25 20:29:02 MXNet C++ Deployment
  8. yuer 发表 2017-10-20 07:52:47 基于leveldb的持久消息队列SDK
  9. yuer 发表 2017-10-07 07:51:32 c++11完美转发
  10. 博主 发表 2016-09-03 00:00:00 C++编译期类型信息的利用
  11. yuer 发表 2017-09-06 03:03:29 libcurl访问unix socket
  12. yuer 发表 2017-09-07 08:14:58 valgrind检测php扩展的warning