c++在字符串中找到指定字符串并取其后面200位
时间: 2023-10-02 20:05:17 浏览: 39
在C++中,可以使用标准库中的find函数和substr函数来实现在字符串中找到指定字符串,并取其后面的200位字符。以下是一个简单的示例程序:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "This is a test. The quick brown fox jumps over the lazy dog.";
// 查找指定字符串
size_t pos = str.find("quick");
if (pos != string::npos) {
// 如果找到了指定字符串,取其后面的200位字符
string result = str.substr(pos + 5, 200);
cout << result << endl;
} else {
cout << "String not found" << endl;
}
return 0;
}
```
在这个示例程序中,我们定义了一个字符串str,然后使用find函数在字符串中查找指定的字符串"quick"。如果找到了指定字符串,我们使用substr函数取其后面的200位字符,并将结果存储在result字符串中。最后,我们打印出result字符串。
需要注意的是,这个示例程序假设指定字符串只出现一次,并且后面至少有200个字符。如果指定字符串出现多次或者后面不足200个字符,需要做一些额外的处理。