C++中的字符串replace()函數替換字符串中[first,last)範圍內的部分中的每個元素。它在 C++ 中的 std::string 類內部定義為提供替換函數的成員函數。
字符串replace()的語法
std::string::replace() 的語法如下:
string& replace( first, last, str);
字符串replace()的參數
- first:指向我們要替換字符串的範圍起始點的迭代器或索引值。
- last:迭代器或索引值到我們要替換字符串的範圍的結束點。
- str:要插入範圍中第一個和最後一個替換當前存在的字符的字符串。
字符串replace()的返回值
- 該函數返回對當前字符串對象(this ptr)的引用。
字符串replace()示例
C++
// CPP code to demonstrate std::string::replace()
#include <iostream>
using namespace std;
// Function for demonstration
void replaceDemo(string s1, string s2, string s3, string s4)
{
// Using positions
cout << "Using positions:"
<< "\n";
// Replaces 7 characters from 0th index by s2
s1.replace(0, 7, s2);
cout << s1 << endl;
// Replaces 3 characters from 0th index with "Hello"
s4.replace(0, 3, "Hello ");
cout << s4 << endl;
// Replaces 5 characters from 6th index of s4 with
// 5 characters from 0th of s3
s4.replace(6, 5, s3, 0, 5);
cout << s4 << endl;
// Replaces 5 characters from 6th index of s4 with
// 6 characters from string "to all"
s4.replace(6, 5, "to all", 6);
cout << s4 << endl;
// Replaces 1 character from 12th index of s4 with
// 3 copies of '!'
s4.replace(12, 1, 3, '!');
cout << s4 << endl;
// Using iterators
cout << "\nUsing iterators:"
<< "\n";
// Replaces whole s2 string with s3
s2.replace(s2.begin(), s2.end(), s3);
cout << s2 << "\n";
// Replaces 13 characters from begin of s1 with string
// "Example"
s1.replace(s1.begin(), s1.begin() + 13, "Example");
cout << s1 << "\n";
// Replace last 7 characters of s4 with first 12
// characters of the string "geeks from- here"
s4.replace(s4.end() - 7, s4.end(), "geeks from- here",
12);
cout << s4 << "\n";
// Replaces last character with complete s2 string from
// s2.begin() till s2.end()
s4.replace(s4.end() - 1, s4.end(), s2.begin(),
s2.end());
cout << s4 << "\n";
// Replaces portion of string s4 starting from 5
// characters from s4.begin() to 15 characters from
// s4.begin() with 2 occurrences of ','
s4.replace(s4.begin() + 5, s4.begin() + 15, 2, ',');
cout << s4 << "\n";
}
// Driver code
int main()
{
string s1 = "Example of replace";
string s2 = "Demonstration";
string s3 = "GeeksforGeeks";
string s4 = "HeyWorld !";
replaceDemo(s1, s2, s3, s4);
return 0;
}
輸出
Using positions: Demonstration of replace Hello World ! Hello Geeks ! Hello to all ! Hello to all!!!! Using iterators: GeeksforGeeks Example of replace Hello to geeks from- Hello to geeks from-GeeksforGeeks Hello,,from-GeeksforGeeks
相關文章: std::string::replace_copy()、std::string::replace_copy_if
本文由薩克希·蒂瓦裏。如果您喜歡 GeeksforGeeks(我們知道您喜歡!)並願意做出貢獻,您還可以使用以下方式撰寫文章write.geeksforgeeks.org或將您的文章郵寄至review-team@geeksforgeeks.org。
相關用法
- C++ std::string::resize()用法及代碼示例
- C++ std::string::rfind用法及代碼示例
- C++ std::string::append()用法及代碼示例
- C++ std::string::assign()用法及代碼示例
- C++ std::string::back()用法及代碼示例
- C++ std::string::clear用法及代碼示例
- C++ std::string::compare()用法及代碼示例
- C++ std::string::data()用法及代碼示例
- C++ std::string::erase用法及代碼示例
- C++ std::string::find_first_not_of用法及代碼示例
- C++ std::string::find_last_not_of用法及代碼示例
- C++ std::string::insert()用法及代碼示例
- C++ std::string::push_back()用法及代碼示例
- C++ std::string::find_last_of用法及代碼示例
- C++ std::strncmp()用法及代碼示例
- C++ std::stable_partition用法及代碼示例
- C++ std::stof用法及代碼示例
- C++ std::stol()、std::stoll()用法及代碼示例
- C++ std::swap()用法及代碼示例
- C++ std::sort()用法及代碼示例
- C++ std::set_difference用法及代碼示例
- C++ std::set_intersection用法及代碼示例
- C++ std::set_union用法及代碼示例
- C++ std::search用法及代碼示例
- C++ std::swap_ranges用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 std::string::replace() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。