Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string view/contains"

From cppreference.com
m (Notes: +s)
m (auto main() -> int -> int main())
Line 1: Line 1:
{{cpp/string/basic_string_view/title | contains}}
+
{{cpp/string/basic_string_view/title|contains}}
 
{{cpp/string/basic_string_view/navbar}}
 
{{cpp/string/basic_string_view/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1 |since=c++23|1=
+
{{dcl|num=1|since=c++23|1=
 
constexpr bool contains( basic_string_view sv ) const noexcept;
 
constexpr bool contains( basic_string_view sv ) const noexcept;
 
}}
 
}}
{{dcl | num=2 |since=c++23|1=
+
{{dcl|num=2|since=c++23|1=
 
constexpr bool contains( CharT c ) const noexcept;
 
constexpr bool contains( CharT c ) const noexcept;
 
}}
 
}}
{{dcl | num=3 |since=c++23|1=
+
{{dcl|num=3|since=c++23|1=
 
constexpr bool contains( const CharT* s ) const;
 
constexpr bool contains( const CharT* s ) const;
 
}}
 
}}
Line 22: Line 22:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | sv | a string view}}
+
{{par|sv|a string view}}
{{par | c | a single character}}
+
{{par|c|a single character}}
{{par | s | a null-terminated character string}}
+
{{par|s|a null-terminated character string}}
 
{{par end}}  
 
{{par end}}  
  
Line 34: Line 34:
  
 
===Example===
 
===Example===
{{example |
+
{{example|
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <string_view>
 
#include <string_view>
  
auto main() -> int
+
int main()
 
{
 
{
 
     using namespace std::literals;
 
     using namespace std::literals;
Line 55: Line 55:
  
 
         // bool contains(const CharT* x) const;
 
         // bool contains(const CharT* x) const;
         << std::string_view("basic_string_view").contains("string") << ' ' // true
+
         << std::string_view("basic_string_view").contains("string") << ' '   // true
         << std::string_view("basic_string_view").contains("String") << ' ' // false
+
         << std::string_view("basic_string_view").contains("String") << '\n'; // false
        << '\n';
+
 
}
 
}
| output=
+
|output=
 
true false true false true false
 
true false true false true false
 
}}
 
}}
Line 65: Line 64:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/basic_string_view/dsc starts_with}}
+
{{dsc inc|cpp/string/basic_string_view/dsc starts_with}}
{{dsc inc | cpp/string/basic_string_view/dsc ends_with}}
+
{{dsc inc|cpp/string/basic_string_view/dsc ends_with}}
{{dsc inc | cpp/string/basic_string_view/dsc find}}
+
{{dsc inc|cpp/string/basic_string_view/dsc find}}
{{dsc inc | cpp/string/basic_string_view/dsc substr}}
+
{{dsc inc|cpp/string/basic_string_view/dsc substr}}
{{dsc inc | cpp/string/basic_string/dsc {{SUBPAGENAMEE}}}}
+
{{dsc inc|cpp/string/basic_string/dsc {{SUBPAGENAMEE}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Revision as of 23:40, 3 June 2023

 
 
 
 
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

Parameters

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

Return value

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

Notes

Feature-test macro Value Std Feature
__cpp_lib_string_contains 202011L (C++23) contains functions

Example

#include <iostream>
#include <string_view>
 
int main()
{
    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") << '\n'; // false
}

Output:

true false true false true false

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]
(C++23)
checks if the string contains the given substring or character
(public member function of std::basic_string<CharT,Traits,Allocator>) [edit]