C String替換 分割指定字串

2021-10-23 18:11:30 字數 930 閱讀 3089

c++的string物件提供了replace方法來實現字串的替換,本文實現對於將字串中某個字串全部替換的功能。

string&

replace_all

(string& str,

const string& strold,

const string& strvalue)

std:

:cout << str<:endl;

return str;

}

根據指定的標識進行分割字串。

void

split

(const string& src,

const string& separator,vector

& dest)

}while

(index != string::npos)

; substring = str.

substr

(start)

;//最後一部分字串(下標為start開始一直到結尾)

dest.

push_back

(substring)

;}

spolt函式引數說名:引數1:要分割的字串;引數2:作為分隔符的字元;引數3:存放分割後的字串的vector向量。

區別:find_first_of 函式最容易出錯的地方是和find函式搞混。它最大的區別就是如果在乙個字串str1中查詢另乙個字串str2,如果str1中含有str2中的***任何字元***,則就會查詢成功,而find則不同;

下面例子在自己跑一邊 就能理解他倆不同之處了。

int

strfind()

cout << str1 << endl;

return0;

}

CString將字元 n 替換為指定字串

最近寫乙個excel批量處理的程式,需要將換行符替換為 r n 但是想了很多辦法卻不能實現,如下 本來是要將 r n 替換為 r n 但是發現從excel或其他檔案中讀取出來的字串在二進位制視窗中發現只有換行符,即 n ascii碼為0a 而回車 r ascii碼為0d 卻沒有了,因此使用cstri...

python替換指定字元

有如下一段文字,要求將中括號後的第乙個分號 替換為換行符 souma,kousaku kanda,fumie masuko,takayoshi tokyo univ agr,fac bioind,abashiri,hokkaido 0992493,japan wang,peng jgfdsa uni...

C string 字串刪除指定字元

今天遇到的情況時需要刪除時間戳裡的 只留下數字。想找比較簡單的實現方法,找了半天,發現的方案 用stl的string的 find 和 erase 首先,通過find找到需要刪除的字元 字串的位置 string str string target int pos str.find target 然後通...