std::basic_...::ends_with(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::basic_string::ends_with β€” C++ Standard Library

ok so bestie, buckle in, because this one is genuinely so cute and useful. it's ends_with, straight out of the C++ Standard Library docs (the ones over at cppreference.com, ily). it does literally what it says on the tin: tells you if a string ends with something. that's it. that's the whole vibe. πŸŽ€


✨ NAME ✨

std::basic_string::ends_with β€” std::basic_string::ends_with πŸ’– (yes the name is the name, we don't make the rules)


🌸 SYNOPSIS 🌸

ok here come the three overloads, all of them since C++20 because she's a modern queen πŸ‘‘

constexpr bool
    ends_with( std::basic_string_view<CharT, Traits> sv ) const
(1) (since C++20)
    noexcept;
constexpr bool
(2) (since C++20)
    ends_with( CharT ch ) const noexcept;
constexpr bool
(3) (since C++20)
    ends_with( const CharT* s ) const;

So what's the actual tea? she checks whether your string ends with the given suffix. and that suffix can be one of these three things:

  1. A string view sv (which, lowkey iconic, may be the result of an implicit conversion from another std::basic_string). πŸ‘‘
  2. A single character ch. πŸ¦‹
  3. A null-terminated character string s. ✨

and here's the slay: all three overloads effectively return std::basic_string_view<CharT, Traits>(data(), size()).ends_with(x), where x is the parameter. so they're all just doing the same little dance underneath. πŸ’…


🧚 PARAMETERS 🧚

  • sv β€” a string view which may be a result of implicit conversion from another std::basic_string
  • ch β€” a single character
  • s β€” a null-terminated character string

πŸ’– RETURN VALUE πŸ’–

true if the string ends with the provided suffix, false otherwise. that's the whole thing. short, sweet, correct. periodt. ✨


πŸ’€ NOTES πŸ’€

there's a feature-test macro for this, and ngl the name is a little goofy but she's valid:

  • __cpp_lib_starts_ends_with β€” value 201711L, std (C++20), feature: String prefix and suffix checking: starts_with() and ends_with()

like yes bestie, starts_with() and ends_with() ship together as a matching set. matching set!! 🫢


πŸ”₯ EXAMPLE πŸ”₯

run this code, it's giving very demure, very assert:

// Run this code

#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)
    );
}

notice how case matters the whole time β€” "C++20!" slays, "c++20!" does not. she is case-sensitive and she is not sorry. πŸ’…


🌈 SEE ALSO 🌈

  • starts_with β€” checks if the string starts with the given prefix (C++20) (public member function)
  • starts_with β€” checks if the string view starts with the given prefix (C++20) (public member function of std::basic_string_view<CharT,Traits>)
  • ends_with β€” checks if the string view ends with the given suffix (C++20) (public member function of std::basic_string_view<CharT,Traits>)
  • contains β€” checks if the string contains the given substring or character (C++23) (public member function)
  • contains β€” checks if the string view contains the given substring or character (C++23) (public member function of std::basic_string_view<CharT,Traits>)
  • compare β€” compares two strings (public member function)
  • substr β€” returns a substring (public member function)

k that's the whole page bestie, go forth and check your suffixes πŸ’œ

source: cppreference.com β€” version dated 2024.06.10 β€” page std::basic_...::ends_with(3), part of the C++ Standard Library manual. mwah, ily, bye πŸŽ€βœ¨

πŸ“¦ where this page comes from