std::basic_string_view<CharT,Traits>::contains
来自cppreference.com
                    
                                        
                    < cpp | string | basic string view
                    
                                                            
                    |   constexpr bool contains( basic_string_view sv ) const noexcept;  | 
(1) | (C++23 起) | 
|   constexpr bool contains( CharT c ) const noexcept;  | 
(2) | (C++23 起) | 
|   constexpr bool contains( const CharT* s ) const;  | 
(3) | (C++23 起) | 
检查字符串视图是否含有给定的子串,其中
1) 子串为字符串视图。
2) 子串为单个字符。
3) 字串为空终止字符串。
所有三个重载都等价于 return find(x) != npos; ,其中 x 为参数。
参数
| sv | - | 字符串视图 | 
| c | - | 单个字符 | 
| s | - | 空终止字符串 | 
返回值
若字符串视图含有给定的子串则为 true ,否则为 false 。
示例
运行此代码
#include <iostream> #include <string_view> auto main() -> int { using namespace std::literals; std::cout << std::boolalpha // bool contains(basic_string_view x) const noexcept; << "https://cppreference.com"sv.contains("cpp"sv) << ' ' // true << "https://cppreference.com"sv.contains("java"sv) << ' ' // false // bool contains(CharT x) const noexcept; << "C++23"sv.contains('+') << ' ' // true << "C++23"sv.contains('-') << ' ' // false // bool contains(const CharT* x) const; << std::string_view("basic_string_view").contains("string") << ' ' // true << std::string_view("basic_string_view").contains("String") << ' ' // false << '\n'; }
输出:
true false true false true false
参阅
|    (C++20)  | 
  检查 string_view 是否始于给定前缀  (公开成员函数)  | 
|    (C++20)  | 
  检查 string_view 是否终于给定后缀  (公开成员函数)  | 
|   在视图中查找字符  (公开成员函数)  | |
|   返回子串  (公开成员函数)  |