c++字符串find,replace,删除 c加加代码举例子
时间: 2023-06-05 22:08:26 浏览: 136
以下是一个使用C++字符串的find、replace和删除操作的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "Hello World!";
// 查找子字符串
size_t pos = str.find("World"); // 返回值为子字符串在原字符串中的位置
if (pos != string::npos)
{
cout << "子字符串 'World' 在原字符串中的位置:" << pos << endl;
}
// 替换子字符串
string new_str = "C++";
str.replace(pos, 5, new_str); // 5表示需要替换的字符个数
cout << "替换后的字符串为:" << str << endl;
// 删除字符
str.erase(0, 6); // 删除原字符串中前6个字符
cout << "删除前6个字符后的字符串为:" << str << endl;
return 0;
}
```
输出结果为:
```
子字符串 'World' 在原字符串中的位置:6
替换后的字符串为:Hello C++!
删除前6个字符后的字符串为:C++!
```
相关问题
c++字符串find
在 C++ 中,`std::string::find()` 函数是一个成员函数,用于在一个字符串中查找指定字符、子串或者是另一个字符串第一次出现的位置。这个函数的基本语法如下:
```cpp
size_t find(const char* str, size_t pos = 0) const;
size_t find(const string& str, size_t pos = 0) const;
```
参数说明:
- `str`: 需要在原字符串中查找的目标,可以是常量字符指针(`const char*`)或另一个字符串引用(`const string&`)。
- `pos`: (可选) 查找的起始位置,默认从字符串开头(`0`)开始。
返回值:
- 如果找到目标子串,函数会返回该子串的第一个字符相对于原字符串的索引(从0开始)。如果没找到,则返回 `npos`(通常定义为 `std::string::npos`,表示找不到的位置)。
示例:
```cpp
std::string myString = "Hello, World!";
size_t index = myString.find("World");
if (index != std::string::npos) {
// 'World' 存在于字符串中,索引位置为6
std::cout << "Found at position: " << index << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
```
c++ 字符串find函数
C++中的字符串类(std::string)提供了find()函数,可以用于查找一个子串在字符串中出现的位置。它的语法如下:
```cpp
size_t find(const string& str, size_t pos = 0) const;
size_t find(const char* s, size_t pos = 0) const;
size_t find(const char* s, size_t pos, size_t n) const;
size_t find(char c, size_t pos = 0) const;
```
其中,第一个参数是要查找的子串,第二个参数是起始查找位置,默认为0。
示例:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello world";
int pos = str.find("world");
if (pos != string::npos) {
cout << "子串 \"world\" 在字符串 \"" << str << "\" 中的位置为:" << pos << endl;
} else {
cout << "子串 \"world\" 未找到!" << endl;
}
return 0;
}
```
输出结果:
```
子串 "world" 在字符串 "hello world" 中的位置为:6
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)