Skip to main content

std::string ends_with() method

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

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

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

Checks if the string ends with the given suffix.

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

std::basic_string_view<CharT, Traits>(data(), size()).ends_with(x)

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 ends with the provided prefix, false otherwise.

Complexity

  • (1) Linear in the size of sv - O(sv.size()).
  • (2) Constant - O(1).
  • (3) Linear in the size of s - O(std::strlen(s)).

Notes

Feature testing macro: __cpp_lib_ends_ends_with.

Example

#include <iostream>
#include <string_view>
#include <string>

template <typename SuffixType>
void test_suffix_print(const std::string& str, SuffixType suffix)
{
std::cout << '\'' << str << "' ends with '" << suffix << "': " <<
str.ends_with(suffix) << '\n';
}

int main()
{
std::boolalpha(std::cout);
auto helloWorld = std::string("hello world");

test_suffix_print(helloWorld, std::string_view("world"));

test_suffix_print(helloWorld, std::string_view("goodbye"));

test_suffix_print(helloWorld, 'd');

test_suffix_print(helloWorld, 'x');
}
Output
'hello world' ends with 'world': true
'hello world' ends with 'goodbye': false
'hello world' ends with 'd': true
'hello world' ends with 'x': false
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 ends_with() method

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

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

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

Checks if the string ends with the given suffix.

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

std::basic_string_view<CharT, Traits>(data(), size()).ends_with(x)

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 ends with the provided prefix, false otherwise.

Complexity

  • (1) Linear in the size of sv - O(sv.size()).
  • (2) Constant - O(1).
  • (3) Linear in the size of s - O(std::strlen(s)).

Notes

Feature testing macro: __cpp_lib_ends_ends_with.

Example

#include <iostream>
#include <string_view>
#include <string>

template <typename SuffixType>
void test_suffix_print(const std::string& str, SuffixType suffix)
{
std::cout << '\'' << str << "' ends with '" << suffix << "': " <<
str.ends_with(suffix) << '\n';
}

int main()
{
std::boolalpha(std::cout);
auto helloWorld = std::string("hello world");

test_suffix_print(helloWorld, std::string_view("world"));

test_suffix_print(helloWorld, std::string_view("goodbye"));

test_suffix_print(helloWorld, 'd');

test_suffix_print(helloWorld, 'x');
}
Output
'hello world' ends with 'world': true
'hello world' ends with 'goodbye': false
'hello world' ends with 'd': true
'hello world' ends with 'x': false
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.