在香港云服务器C++中,string.find()函数用于在字符串中搜索指定的子字符串,并返回第一次出现的位置索引。如果找到子字符串,则返回第一次出现的位置索引;如果未找到,则返回string::npos。
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string subStr = "world";
size_t found = str.find(subStr);
if (found != std::string::npos) {
std::cout << "Substring found at index: " << found << std::endl;
} else {
std::cout << "Substring not found" << std::endl;
}
return 0;
}
复制代码
在上面的示例中,我们首先定义了一个字符串str和一个子字符串subStr。然后,我们使用string.find()函数在字符串str中搜索子字符串subStr,并将结果存储在变量found中。最后,我们检查found的值,如果不等于string::npos,则说明找到了子字符串,打印出子字符串在字符串中的位置索引;否则,打印出子字符串未找到的信息。
一诺网络香港免备案专区,提供「香港增强云服务器」和「香港特惠云服务器」两种类型的高可用弹性计算服务,搭载新一代英特尔®至强®铂金处理器,接入CN2低延时高速回国带宽线路,网络访问顺滑、流畅。机房网络架构采用了BGP协议的解决方案可提供多线路互联融合网络,使得不同网络运营商线路的用户都能通过最佳路由实现快速访问。香港云服务器低至29元/月,购买链接:https://www.enuoidc.com/vps.html?typeid=2