#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
// 使用 replace 函数将 "world" 替换为 "C++"
str.replace(str.find("world"), 5, "C++");
std::cout << str << std::endl; // 输出: Hello, C++!
return 0;
}
std::string
是 C++ 标准库中的字符串类,用于处理字符串。str.replace(pos, len, new_str)
是 std::string
类的一个成员函数,用于将从位置 pos
开始的 len
个字符替换为新的字符串 new_str
。str.find("world")
用于查找子字符串 "world" 在字符串 str
中的位置。如果找到,则返回其起始位置;否则返回 std::string::npos
。如果你需要更多关于 replace
的用法或有其他问题,请告诉我!
上一篇:c++编程教程
下一篇:c++ char*转string
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站