std::basic_string_view<CharT,Traits>::contains
来自 cppreference.cn
< cpp | string | basic string view
constexpr bool contains( basic_string_view sv ) const noexcept; |
(1) | (since C++23) |
constexpr bool contains( CharT c ) const noexcept; |
(2) | (since C++23) |
constexpr bool contains( const CharT* s ) const; |
(3) | (since C++23) |
检查字符串视图是否包含给定的子字符串,其中
1) 子字符串是一个字符串视图。
2) 子字符串是一个单字符。
3) 子字符串是一个空终止的字符字符串。
所有三个重载等价于 return find(x) != npos;,其中 x
是形参。
内容 |
[edit] 参数
sv | - | 一个字符串视图 |
c | - | 一个单字符 |
s | - | 一个空终止的字符字符串 |
[edit] 返回值
true 如果字符串视图包含提供的子字符串,则为 false,否则为否。
[edit] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_string_contains |
202011L |
(C++23) | contains 函数 |
[edit] 示例
运行此代码
#include <string_view> using namespace std::literals; static_assert ( // bool contains(basic_string_view x) const noexcept; "https://cppreference.cn"sv.contains("cpp"sv) == true and "https://cppreference.cn"sv.contains("php"sv) == false and // bool contains(CharT x) const noexcept; "C++23"sv.contains('+') == true and "C++23"sv.contains('-') == false and // bool contains(const CharT* x) const; std::string_view("basic_string_view").contains("string") == true and std::string_view("basic_string_view").contains("String") == false ); int main() {}
[edit] 参见
(C++20) |
检查字符串视图是否以给定前缀开头 (public member function) |
(C++20) |
检查字符串视图是否以给定后缀结尾 (public member function) |
在视图中查找字符 (public member function) | |
返回子字符串 (public member function) | |
(C++23) |
检查字符串是否包含给定的子字符串或字符 (public member function of std::basic_string<CharT,Traits,Allocator> ) |