- C 标准库
- C 标准库
- C++ 标准库
- C++ 库 - 主页
- C++ 库 - <fstream>
- C++ 库 - <iomanip>
- C++ 库 - <ios>
- C++ 库 - <iosfwd>
- C++ 库 - <iostream>
- C++ 库 - <istream>
- C++ 库 - <ostream>
- C++ 库 - <sstream>
- C++ 库 - <streambuf>
- C++ 库 - <原子>
- C++ 库 - <复杂>
- C++ 库 - <异常>
- C++ 库 - <功能>
- C++ 库 - <限制>
- C++ 库 - <语言环境>
- C++ 库 - <内存>
- C++ 库 - <新>
- C++ 库 - <数字>
- C++ 库 - <正则表达式>
- C++ 库 - <stdexcept>
- C++ 库 - <字符串>
- C++ 库 - <线程>
- C++ 库 - <元组>
- C++ 库 - <类型信息>
- C++ 库 - <实用程序>
- C++ 库 - <valarray>
C++ 字符串库 - 替换
描述
它替换字符串中从字符 pos 开始并跨越 len 个字符的部分。
宣言
以下是 std::string::replace 的声明。
string& replace (size_t pos, size_t len, const string& str,
size_t subpos, size_t sublen);
C++11
string& replace (size_t pos,size_t len,const string& str,
size_t subpos, size_t sublen);
C++14
string& replace (size_t pos,size_t len,const string& str,
size_t subpos, size_t sublen = npos);
参数
pos - 这是一个插入点。
str - 它是一个字符串对象。
len - 它包含有关要删除的字符数的信息。
返回值
它返回*this。
例外情况
如果抛出异常,则字符串不会发生任何变化。
例子
在下面的 std::string::replace 示例中。
#include <iostream>
#include <string>
int main () {
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
std::string str=base;
str.replace(9,5,str2);
str.replace(19,6,str3,7,6);
str.replace(8,10,"just a");
str.replace(8,6,"a shorty",7);
str.replace(22,1,3,'!');
str.replace(str.begin(),str.end()-3,str3);
str.replace(str.begin(),str.begin()+6,"replace");
str.replace(str.begin()+8,str.begin()+14,"is coolness",7);
str.replace(str.begin()+12,str.end()-4,4,'o');
str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());
std::cout << str << '\n';
return 0;
}
示例输出应该是这样的 -
replace is useful.
字符串.htm