std::basic_...ng::contains(3)
β¨ NAME β¨
okay so bestie, this is the page for std::basic_string::contains β std::basic_string::contains, exactly like the NAME says (twice, because slay π
). The title is std::basic_...ng::contains(3), and the manual name is C++ Standard Libary (yep, the header spells it βLibaryβ and we are not fixing it). This is lowkey the βdoes my string contain this substring?β queen. π
π Synopsis π
three overloads, all constexpr, all here since C++23 β check the noexcept placement:
constexpr bool
contains( std::basic_string_view<CharT,Traits> sv ) const
(1) (since C++23)
noexcept;
constexpr bool
(2) (since C++23)
contains( CharT ch ) const noexcept;
constexpr bool
(3) (since C++23)
contains( const CharT* s ) const;
Checks if the string contains the given substring. The substring may be one of the following:
- A string view
sv(which may be a result of implicit conversion from anotherstd::basic_string). - A single character
ch. - A null-terminated character string
s.
All three overloads are equivalent to return find(x) != npos;, where x is the parameter. no cap, that's the whole trick β¨
π Parameters π
svβ a string view which may be a result of implicit conversion from anotherstd::basic_stringchβ a single charactersβ a null-terminated character string
π Return value π
true if the string contains the provided substring, false otherwise. that's the whole answer, bestie π
πΈ Notes πΈ
- Feature-test macro
__cpp_lib_string_containsβ Value202011L, Std (C++23), Feature contains functions
π¦ Example π¦
// Run this 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');
}
π¨οΈ Output π¨οΈ
"hello world" contains "hello"
"hello world" does not contain "goodbye"
"hello world" contains 'w'
"hello world" does not contain 'x'
π See also π
starts_withβ checks if the string starts with the given prefix (C++20) (public member function)ends_withβ checks if the string ends with the given suffix (C++20) (public member function)findβ finds the first occurrence of the given substring (public member function)substrβ returns a substring (public member function)containsβ checks if the string view contains the given substring or character (C++23) (public member function ofstd::basic_string_view<CharT,Traits>)
mwah π β source: http://cppreference.com, version/date: 2024.06.10, and this iconic page is std::basic_...ng::contains(3). bye bestie π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms