std::basic_string_view<CharT,Traits>::contains
来自 zh.cppreference.net
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 则表示不包含。
注释
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_string_contains
|
202011L
|
(C++23) |
contains
函数
|
示例
运行此代码
#include <string_view> using namespace std::literals; static_assert ( // bool contains(basic_string_view x) const noexcept; "https://zh.cppreference.net"sv.contains("cpp"sv) == true and "https://zh.cppreference.net"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() {}
另请参阅
|
(C++20)
|
检查字符串视图是否以给定前缀开头
(公开成员函数) |
|
(C++20)
|
检查字符串视图是否以给定后缀结尾
(公开成员函数) |
|
在视图中查找字符
(公开成员函数) |
|
|
返回子字符串
(公开成员函数) |
|
|
(C++23)
|
检查字符串是否包含给定子串或字符
(
std::basic_string<CharT,Traits,Allocator>
的公开成员函数)
|