Namespaces
Variants
Views
Actions

std::basic_string_view<CharT,Traits>::contains

From cppreference.com
 
 
 
 
constexpr bool contains( basic_string_view sv ) const noexcept;
(1) (since C++23)
constexpr bool contains( CharT c ) const noexcept;
(2) (since C++23)
constexpr bool contains( const CharT* s ) const;
(3) (since C++23)

Checks if the string view contains the given substring, where

1) the substring is a string view.
2) the substring is a single character.
3) the substring is a null-terminated character string.

All three overloads are equivalent to return find(x) != npos;, where x is the parameter.

Contents

[edit] Parameters

sv - a string view
c - a single character
s - a null-terminated character string

[edit] Return value

true if the string view contains the provided substring, false otherwise.

[edit] Example

#include <iostream>
#include <string_view>
 
auto main() -> int
{
    using namespace std::literals;
 
    std::cout
        << std::boolalpha
 
        // bool contains(basic_string_view x) const noexcept;
        << "https://cppreference.com"sv.contains("cpp"sv)  << ' ' // true
        << "https://cppreference.com"sv.contains("java"sv) << ' ' // false
 
        // bool contains(CharT x) const noexcept;
        << "C++23"sv.contains('+') << ' ' // true
        << "C++23"sv.contains('-') << ' ' // false
 
        // bool contains(const CharT* x) const;
        << std::string_view("basic_string_view").contains("string") << ' ' // true
        << std::string_view("basic_string_view").contains("String") << ' ' // false
        << '\n';
}

Output:

true false true false true false

[edit] See also

checks if the string view starts with the given prefix
(public member function) [edit]
(C++20)
checks if the string view ends with the given suffix
(public member function) [edit]
find characters in the view
(public member function) [edit]
returns a substring
(public member function) [edit]