Skip to main content

std::string_view 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 view ends with the given suffix, where

  • (1) The suffix is a string view.
    Effectively returns size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0.

  • (2) The suffix is a single character.
    Effectively returns !empty() && Traits::eq(back(), c).

  • (3) The suffix is a null-terminated character string.
    Effectively returns ends_with(basic_string_view(s)).

Parameters

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

Return value

true if the view ends with the provided suffix, 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>

auto main() -> int
{
using namespace std::literals;

std::cout
<< std::boolalpha

// (1) bool ends_with( basic_string_view sv ) const noexcept;
<< std::string_view("https://cppreference.com").ends_with(".com"sv) << ' ' // true
<< std::string_view("https://cppreference.com").ends_with(".org"sv) << ' ' // false

// (2) bool ends_with( CharT c ) const noexcept;
<< std::string_view("C++20").ends_with('0') << ' ' // true
<< std::string_view("C++20").ends_with('3') << ' ' // false

// (3) bool ends_with( const CharT* s ) const;
<< std::string_view("string_view").ends_with("view") << ' ' // true
<< std::string_view("string_view").ends_with("View") << ' ' // false
<< '\n';
}
Output
true false true false true 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_view 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 view ends with the given suffix, where

  • (1) The suffix is a string view.
    Effectively returns size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0.

  • (2) The suffix is a single character.
    Effectively returns !empty() && Traits::eq(back(), c).

  • (3) The suffix is a null-terminated character string.
    Effectively returns ends_with(basic_string_view(s)).

Parameters

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

Return value

true if the view ends with the provided suffix, 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>

auto main() -> int
{
using namespace std::literals;

std::cout
<< std::boolalpha

// (1) bool ends_with( basic_string_view sv ) const noexcept;
<< std::string_view("https://cppreference.com").ends_with(".com"sv) << ' ' // true
<< std::string_view("https://cppreference.com").ends_with(".org"sv) << ' ' // false

// (2) bool ends_with( CharT c ) const noexcept;
<< std::string_view("C++20").ends_with('0') << ' ' // true
<< std::string_view("C++20").ends_with('3') << ' ' // false

// (3) bool ends_with( const CharT* s ) const;
<< std::string_view("string_view").ends_with("view") << ' ' // true
<< std::string_view("string_view").ends_with("View") << ' ' // false
<< '\n';
}
Output
true false true false true 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.