c 中replace函式用法總結

2021-07-22 01:51:21 字數 1925 閱讀 9518

一、用法一

string& replace (size_t pos, size_t len, const string& str) 用str 替換指定字串從起始位置pos開始長度為len 的字元

replace(i, j, str);    用str 替換從i 開始的長度為 j 的字元

例子:

#includeusing namespace std;

int main(){

string line="hello world, i love python!";

string s = line.replace(line.find("i"), 1, "haha");

cout輸出

二、用法二

string& replace (const_iterator i1, const_iterator i2, const string& str);

用str替換 迭代器起始位置和結束位置的字元

例子:

#includeusing namespace std;

int main(){

string line="hello world, i love python!";

string s = line.replace(line.begin(), line.begin()+5, "haha");

cout<

輸出:三、用法三

string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

用substr 的指定子串(給定起始位置和長度)替換從指定位置上的字串

例子:

#includeusing namespace std;

int main(){

string line="hello world, i love python!";

string substr="12345";

string s = line.replace(0, 5, substr, substr.find("2"), 3);

cout輸出:

四、用法四

string& replace (size_t pos, size_t len, size_t n, char c);

用重複n 次的c 字元替換從指定位置 pos 長度為 len 的內容

例子:

#includeusing namespace std;

int main(){

string line="hello world, i love python!";

char substr='1';

string s = line.replace(0, 5, 3, substr);

cout<

輸出:

五、用法五

string& replace (const_iterator i1, const_iterator i2, size_t n, char c)

用重複 n 次的 c 字元替換從指定迭代器位置(從 i1 開始到結束)的內容

例子:

#includeusing namespace std;

int main(){

string line="hello world, i love python!";

char substr='1';

string s = line.replace(line.begin(), line.begin()+5, 3, substr);

cout輸出:

SQL 中函式 REPLACE 的用法及例項

一 定義 官方語法 replace string expression string pattern string replacement 引數含義 string expression 要搜尋的字串表示式。string expression 可以是字元或二進位制資料型別。string pattern...

js中replace的用法

replace方法的語法是 stringobj.replace rgexp,replacetext 其中stringobj是字串 string reexp可以是正規表示式物件 regexp 也可以是字串 string replacetext是替代查詢到的字串。為了幫助大家更好的理解,下面舉個簡單例子...

mySQL中replace的用法

mysql replace例項說明 update tb1 set f1 replace f1,abc def replace str,from str,to str 在字串 str 中所有出現的字串 from str 均被 to str替換,然後返回這個字串 這個函式用來批量替換資料中的非法關鍵字是...