std::basic_string_view<CharT,Traits>::contains
来自 cppreference.cn
< 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) 子字符串是一个以 null 结尾的字符字符串。
所有三个重载都等价于 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://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() {}
[编辑] 参阅
| (C++20) |
检查字符串视图是否以给定前缀开头 (public 成员函数) |
| (C++20) |
检查字符串视图是否以给定后缀结尾 (public 成员函数) |
| 在视图中查找字符 (public 成员函数) | |
| 返回子字符串 (public 成员函数) | |
| (C++23) |
检查字符串是否包含给定子字符串或字符 ( std::basic_string<CharT,Traits,Allocator> 的 public 成员函数) |