std::basic_string<CharT,Traits,Allocator>::ends_with

Checks if the string ends with the given suffix. The suffix may be one of the following:

# Declarations

constexpr bool
ends_with( std::basic_string_view<CharT, Traits> sv ) const noexcept;

(since C++20)

constexpr bool
ends_with( CharT ch ) const noexcept;

(since C++20)

constexpr bool
ends_with( const CharT* s ) const;

(since C++20)

# Parameters

# Return value

true if the string ends with the provided suffix, false otherwise.

# Notes

Feature-test macro Value Std Feature __cpp_lib_starts_ends_with 201711L (C++20) String prefix and suffix checking: starts_with() and ends_with()

# Example

#include <cassert>
#include <string>
#include <string_view>
 
int main()
{
    using namespace std::literals;
 
    const auto str = "Hello, C++20!"s;
 
    assert
    (""
        && str.ends_with("C++20!"sv)  // (1)
        && !str.ends_with("c++20!"sv) // (1)
        && str.ends_with("C++20!"s)   // (1) implicit conversion string to string_view
        && !str.ends_with("c++20!"s)  // (1) implicit conversion string to string_view
        && str.ends_with('!')         // (2)
        && !str.ends_with('?')        // (2)
        && str.ends_with("C++20!")    // (3)
        && !str.ends_with("c++20!")   // (3)
    );
}

# See also