std::basic_string<CharT,Traits,Allocator>::contains
Depuis fr.cppreference.net
constexpr bool
contains( std::basic_string_view<CharT,Traits> sv ) const noexcept;
|
(1) | (depuis C++23) |
constexpr bool
contains( CharT ch ) const noexcept;
|
(2) | (depuis C++23) |
constexpr bool
contains( const CharT* s ) const;
|
(3) | (depuis C++23) |
Vérifie si la chaîne contient la sous-chaîne donnée. La sous-chaîne peut être l'une des suivantes :
1) Une vue de chaîne
sv (qui peut être le résultat d'une conversion implicite à partir d'un autre std::basic_string).2) Un caractère unique
ch.3) Une chaîne de caractères terminée par un caractère nul
s.Les trois surcharges sont équivalentes à return find(x) != npos;, où x est le paramètre.
Paramètres
| sv | - |
une vue de chaîne qui peut résulter d'une conversion implicite d'un autre
std::basic_string
|
| ch | - | un caractère unique |
| s | - | une chaîne de caractères terminée par un caractère nul |
Valeur de retour
true si la chaîne contient la sous-chaîne fournie, false sinon.
Notes
| Macro de test de fonctionnalité | Valeur | Std | Fonctionnalité |
|---|---|---|---|
__cpp_lib_string_contains
|
202011L
|
(C++23) |
contains
fonctions
|
Exemple
Exécuter ce code
#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'); }
Sortie :
"hello world" contains "hello" "hello world" does not contain "goodbye" "hello world" contains 'w' "hello world" does not contain 'x'
Voir aussi
|
(C++20)
|
vérifie si la chaîne commence par le préfixe donné
(fonction membre publique) |
|
(C++20)
|
vérifie si la chaîne se termine par le suffixe donné
(fonction membre publique) |
|
trouve la première occurrence de la sous-chaîne donnée
(fonction membre publique) |
|
|
retourne une sous-chaîne
(fonction membre publique) |
|
|
(C++23)
|
vérifie si la vue de chaîne contient la sous-chaîne ou le caractère donné
(fonction membre publique de
std::basic_string_view<CharT,Traits>
)
|