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) 子字串是一個以 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.tw"sv.contains("cpp"sv) == true and "https://cppreference.tw"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 成員函式) |