std::basic_...::ends_with(3)
π 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:
- A string view
sv(which, lowkey iconic, may be the result of an implicit conversion from anotherstd::basic_string). π - A single character
ch. π¦ - 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 anotherstd::basic_stringchβ a single charactersβ 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β value201711L, std(C++20), feature: String prefix and suffix checking:starts_with()andends_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 ofstd::basic_string_view<CharT,Traits>)ends_withβ checks if the string view ends with the given suffix (C++20) (public member function ofstd::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 ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms