如何将一个字符串放在另一个字符串中,而不是将字符放在某个索引处
How to place a string inside another string , instead of the character at some index
如果我有:Enter(x)
并且我想在(
后面放置room2
,但是没有x
,那么std::insert不起作用自if:
std::string myString = "Enter(x)";
std::string placeMe = "room2";
myString.insert(myString.find_first_of("(") + 1 , placeMe);
那么我得到:Enter(room2x)
。
如何将placeMe
替换为某个索引的 ?
我相信你正在寻找的功能是替换而不是插入。
http://www.cplusplus.com/reference/string/string/replace/首先,您可能想要find
而不是find_first_of
,因为您只想匹配一个可能的子字符串。其次,您需要std::string::replace
函数:
myString.replace(myString.find("(") + 1, 1, placeMe);
相关文章:
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 尝试将字符串/字符转换为整数会产生意外结果
- 为什么没有访问所有字符串字符?
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- 如何将字符串字符与结构数组进行比较?
- 将单个字符串/字符输入串行监视器
- 读取字符串字符时出错 - 对于数组中的字符串值
- C++ C 样式字符串/字符数组的大小 - 优化
- 读取字符串字符时出错
- C++ 使用 Strtok 读取字符串字符时出错
- 如何将 int 数组的 1 个元素转换为字符串/字符
- 在另一个字符串中逐个复制字符串字符
- 将字符串字符映射到矢量
- MBED RTOS将Float转换为字符串/字符阵列
- 如何使用字符串字符数组中的 ++ 运算符访问下一个字符串
- 如何比较C++中的单个字符串字符?
- 内部字符串/字符如何在 int 和 float 中存储
- 将十六进制转换为可打印的字符串/字符
- wcscpy_s后读取字符串字符时出错
- 检查字符串字符和长度