1. 程式人生 > >C++ string的替換字元

C++ string的替換字元

C++的string提供了replace方法來實現字串的替換,但是對於將字串中某個字串全部替換這個功能,string並沒有實現,我們今天來做的就是這件事。
首先明白一個概念,即string替換所有字串,將"12212"這個字串的所有"12"都替換成"21",結果是什麼?
可以是22211,也可以是21221,有時候應用的場景不同,就會希望得到不同的結果,所以這兩種答案都做了實現,程式碼如下:

[cpp] view plaincopyprint?
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. string& replace_all(string& str,const string& old_value,const string& new_value)
  5. {
  6. while(true) {
  7. string::size_type pos(0);
  8. if( (pos=str.find(old_value))!=string::npos )
  9. str.replace(pos,old_value.length(),new_value);
  10. else break;
  11. }
  12. return str;
  13. }
  14. string& replace_all_distinct(string& str,const
    string& old_value,const string& new_value)
  15. {
  16. for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()) {
  17. if( (pos=str.find(old_value,pos))!=string::npos )
  18. str.replace(pos,old_value.length(),new_value);
  19. else break;
  20. }
  21. return str;
  22. }
  23. int main()
  24. {
  25. cout << replace_all(string("12212"
    ),"12","21") << endl;
  26. cout << replace_all_distinct(string("12212"),"12","21") << endl;
  27. }
  28. /*
  29. 輸出如下:
  30. 22211
  31. 21221
  32. */
#include   <string>   
#include   <iostream>   
using   namespace   std;   
string&   replace_all(string&   str,const   string&   old_value,const   string&   new_value)   
{   
    while(true)   {   
        string::size_type   pos(0);   
        if(   (pos=str.find(old_value))!=string::npos   )   
            str.replace(pos,old_value.length(),new_value);   
        else   break;   
    }   
    return   str;   
}   
string&   replace_all_distinct(string&   str,const   string&   old_value,const   string&   new_value)   
{   
    for(string::size_type   pos(0);   pos!=string::npos;   pos+=new_value.length())   {   
        if(   (pos=str.find(old_value,pos))!=string::npos   )   
            str.replace(pos,old_value.length(),new_value);   
        else   break;   
    }   
    return   str;   
}   
int   main()   
{   
    cout   <<   replace_all(string("12212"),"12","21")   <<   endl;   
    cout   <<   replace_all_distinct(string("12212"),"12","21")   <<   endl;   
}   
/*
輸出如下:   
22211   
21221
*/

相關推薦

C++ string替換字元

C++的string提供了replace方法來實現字串的替換,但是對於將字串中某個字串全部替換這個功能,string並沒有實現,我們今天來做的就是這件事。 首先明白一個概念,即string替換所有字串,將"12212"這個字串的所有"12"都替換成"21",結果是什麼? 可

C String理解—— 字符串替換函數

rcp wstring cnblogs repl 指定 bcd str right stdio.h 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4

string替換所有指定字符串(C++)

con pos c++ clas 兩種 esp wan replace fin C++的string提供了replace方法來實現字符串的替換,但是對於將字符串中某個字符串全部替換這個功能,string並沒有實現,我們今天來做的就是這件事。首先明白一個概念,即string替

C++中std::stringC-String字元陣列的互相轉換

C語言中只有字元陣列這一說法,沒有C++專門封裝的字串類std::string。而字元陣列C-String以\0作為結束符。std::string其實還是儲存了C-String這個指標,只不過不同的編譯期對std::string中的儲存結構都做了不同的處理,這裡我們不討論std::str

自定義擴充套件js函式庫---string.replaceAll()---字元替換所有指定字元

/* * 自定義擴充套件js函式庫 * @time:181203 * @add : string.replaceAll()---字元替換所有指定字元 */ /* * string.replaceAll()---字元替換所有指定字元 * @parameter: FindTex

C++ string字元中英文字母大小寫的轉換

c++中的string類物件並沒有自帶的方法進行字元大小寫轉換,進行大小寫轉換的方法很多,這裡我們提供一個通過algorithm中的transform函式對string物件進行字元的大小寫轉換。 #include <iostream> #include <string&g

c++快速替換字串指定字元

   專案中偶爾要用到替換指定字元的功能,因為不常用每次一用到的就去官方文件看,總得花點兒時間感覺不是那麼好用,自己想了個辦法,可能效能不是最好的但我感覺應該是最直觀明瞭的;以下是實現: #inclu

如何替換字元陣列中的 某些字元C/C++

用新字元,替換字元陣列中的某個字元的過程: 遍歷陣列,迴圈檢查每一個字元, 如果與待替換字元相同,則進行替換 否則,繼續檢查下一個 迴圈結束,輸出替換後的字串 參考程式碼: #include <stdio.h> void main( int 

c++ string 末尾追加char字元

如果想在string s末尾追加abc s = s + 'a' + 'b' + 'c'; //這樣寫是可以的 s += 'a' + 'b' + 'c'; //這樣寫是不對的所以追加單個字元 可以一個個

string替換所有指定字串(C++)

C++的string提供了replace方法來實現字串的替換,但是對於將字串中某個字串全部替換這個功能,string並沒有實現,我們今天來做的就是這件事。首先明白一個概念,即string替換所有字串,將"12212"這個字串的所有"12"都替換成"21",結果是什麼? 可以

c# String.Join 和 Distinct 方法 去除字串中重複字元

1.在寫程式中經常操作字串,需要去重,以前我的用方式利用List集合和 contains去重複資料程式碼如下: 1 string test="123,123,32,125,68,9565,432,6543,343,32,125,68"; 2 string[] array = test.Spli

C++string:查詢、替換、插入、刪除等

string的連線: string &operator+=(const string &s);//把字串s連線到當前字串的結尾  string &append(const char *s);            //把c型別字串s連線到當前字串結尾 string &appen

c: string 字元指標與字元陣列的區分

來看一段簡單的程式碼,以區分字元指標與字元陣列在表達字串時的一些區別。 #include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) {

C# List泛型轉換,int,string字元,轉陣列各種型別的轉換

List轉字串 1  2 List<string> List = new List<string>(); string strArray = string.Join(",", List); 字

c#替換字元的多個空格多個換行及中文字元

            xml = xml.Replace(" ", " ");             xml = xml.Replace("\t", "");             xml = Regex.Replace(xml, "[\r]{2,}", "\n");             xml =

c++ string wstring 字串替換

c++的string本身沒有字串替換為另外一個字串的功能,特此在這裡新增一個替換函式 int CStringTool::Replace(std::wstring& strContent, std::wstring& strReplace, std::wstr

自己編寫java中String替換指定位置的字元的方法

static String replace(String mystring,String content, int location){//mystring 為原來字串,content為新增的字元,location為新增的位置return mystring.substring

利用C++ string實現任意長度小數、整數之間的加法

code include spa ctype empty esp pac space temp 1 /* 2 大數的運算1--加法: 3 利用C++ string實現任意長度小數、整數之間的加法 4 作者:大大維 5 2017/5/5

C++string中用於查找的find系列函數淺析

ace 規則 ret stream num 原因 目標 自己 查找字符 總述: 以下所講的所有的string查找函數,都有唯一的返回類型,那就是size_type,即一個無符號整數(按打印出來的算)。若查找成功,返回按查找規則找到的第一個字符或子串的位置;若查找失

C++ string中的幾個小陷阱,你掉進過嗎?

stl 試題 賦值 clu ror ati world mod iostream C++開發的項目難免會用到STL的string。使用管理都比char數組(指針)方便的多。但在得心應手的使用過程中也要警惕幾個小陷阱。避免我們項目出bug卻遲遲找不到原因。1. 結構體中的