Skip to main content

std::string contains() method

// (1) Const version only
constexpr bool contains( std::basic_string_view<CharT,Traits> sv ) const noexcept;

// (2) Const version only
constexpr bool contains( CharT c ) const noexcept;

// (3) Const version only
constexpr bool contains( const CharT* s ) const;

Checks if the string contains the given substring

The prefix may be one of the following:

  • (1) A string view sv (which may be a result of implicit conversion from another std::basic_string).

  • (2) A single character c.

  • (3) A null-terminated character string s.

All three overloads effectively do

find(x) != npos;

where x is the parameter.

Parameters

  • s - a string view which may be a result of implicit conversion from another std::basic_string
  • c - a single character
  • s - a null-terminated character string

Return value

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

Complexity

important

This section requires improvement. You can help by editing this doc page.

Notes

Feature testing macro: __cpp_lib_string_contains.

Example

#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'
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string contains() method

// (1) Const version only
constexpr bool contains( std::basic_string_view<CharT,Traits> sv ) const noexcept;

// (2) Const version only
constexpr bool contains( CharT c ) const noexcept;

// (3) Const version only
constexpr bool contains( const CharT* s ) const;

Checks if the string contains the given substring

The prefix may be one of the following:

  • (1) A string view sv (which may be a result of implicit conversion from another std::basic_string).

  • (2) A single character c.

  • (3) A null-terminated character string s.

All three overloads effectively do

find(x) != npos;

where x is the parameter.

Parameters

  • s - a string view which may be a result of implicit conversion from another std::basic_string
  • c - a single character
  • s - a null-terminated character string

Return value

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

Complexity

important

This section requires improvement. You can help by editing this doc page.

Notes

Feature testing macro: __cpp_lib_string_contains.

Example

#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'
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.