检查字符串是否包含C ++中的字符串 [英] Check if a string contains a string in C++

查看:165
本文介绍了检查字符串是否包含C ++中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型字符串的变量。我想检查它是否包含某个字符串。

I have a variable of type string. I want to check if it contains a certain string. How would I do that?

是否有一个函数,如果找到字符串则返回true,如果不是,则返回false?

Is there a function that returns true if the string is found, and false if it isn't?

推荐答案

使用 std :: string :: find 如下:

if (s1.find(s2) != std::string::npos) {
    std::cout << "found!" << '\n';
}

注意:found!将打印如果s2是s1的子字符串,则s1和s2都是std :: string类型。

Note: "found!" will be printed if s2 is a substring of s1, both s1 and s2 are of type std::string.

这篇关于检查字符串是否包含C ++中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆