std::basic_string<CharT,Traits,Allocator>::contains
ja.cppreference.net より
constexpr bool
contains( std::basic_string_view<CharT,Traits> sv ) const noexcept;
|
(1) | (C++23以上) |
constexpr bool
contains( CharT ch ) const noexcept;
|
(2) | (C++23以上) |
constexpr bool
contains( const CharT* s ) const;
|
(3) | (C++23以上) |
文字列が指定された部分文字列を含むかどうかを調べます。部分文字列は以下のいずれかです。
1) 文字列ビュー
sv (別の std::basic_string からの暗黙の変換の結果である場合もあります)。2) 単一の文字
ch。3) ヌル終端文字列
s。3つのオーバーロードはすべて return find(x) != npos; と同等です。ここで、x はパラメータです。
パラメータ
| sv | - |
他の
std::basic_string
からの暗黙変換の結果である可能性がある文字列ビュー
|
| ch | - | 単一の文字 |
| s | - | ヌル終端文字列 |
戻り値
true 文字列が指定された部分文字列を含む場合、 false それ以外の場合。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_string_contains
|
202011L
|
(C++23) |
contains
関数
|
例
このコードを実行
#include <iomanip> #include <iostream> #include <string> #include <string_view> #include <type_traits> template<typename SubstrType> void test_substring(const std::string& str, SubstrType subs) { constexpr char delim = std::is_scalar_v<SubstrType> ? '\'' : '\"'; std::cout << std::quoted(str) << (str.contains(subs) ? " contains " : " does not contain ") << std::quoted(std::string{subs}, delim) << '\n'; } int main() { using namespace std::literals; auto helloWorld = "hello world"s; test_substring(helloWorld, "hello"sv); test_substring(helloWorld, "goodbye"sv); test_substring(helloWorld, 'w'); test_substring(helloWorld, 'x'); }
出力:
"hello world" contains "hello" "hello world" does not contain "goodbye" "hello world" contains 'w' "hello world" does not contain 'x'
関連項目
|
(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_view<CharT,Traits>
)
|