Namespaces
Variants

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

From cppreference.net
std::basic_string
constexpr bool
ends_with ( std:: basic_string_view < CharT, Traits > sv ) const noexcept ;
(1) (C++20以降)
constexpr bool
ends_with ( CharT ch ) const noexcept ;
(2) (C++20以降)
constexpr bool
ends_with ( const CharT * s ) const ;
(3) (C++20以降)

文字列が指定された接尾辞で終わるかどうかをチェックします。接尾辞は以下のいずれかになります:

1) 文字列ビュー sv (これは他の std::basic_string からの暗黙変換の結果である可能性があります)。
2) 単一の文字 ch .
3) ヌル終端文字列 s .

3つのオーバーロードはすべて、実質的に std:: basic_string_view < CharT, Traits > ( data ( ) , size ( ) ) . ends_with ( x ) を返します。ここで x はパラメータです。

目次

パラメータ

sv - 他の std::basic_string からの暗黙変換の結果である可能性のある文字列ビュー
ch - 単一の文字
s - ヌル終端文字列

戻り値

true 文字列が指定された接尾辞で終わる場合、 false それ以外の場合。

注記

機能テスト マクロ 標準 機能
__cpp_lib_starts_ends_with 201711L (C++20) 文字列の接頭辞と接尾辞のチェック: starts_with() および ends_with()

#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) 暗黙的な string から string_view への変換
        && !str.ends_with("c++20!"s)  // (1) 暗黙的な string から string_view への変換
        && str.ends_with('!')         // (2)
        && !str.ends_with('?')        // (2)
        && str.ends_with("C++20!")    // (3)
        && !str.ends_with("c++20!")   // (3)
    );
}

関連項目

文字列が指定された接頭辞で始まるかどうかをチェックする
(公開メンバ関数)
文字列ビューが指定された接頭辞で始まるかどうかをチェックする
( std::basic_string_view<CharT,Traits> の公開メンバ関数)
(C++20)
文字列ビューが指定された接尾辞で終わるかどうかをチェックする
( std::basic_string_view<CharT,Traits> の公開メンバ関数)
(C++23)
文字列が指定された部分文字列または文字を含むかどうかをチェックする
(公開メンバ関数)
(C++23)
文字列ビューが指定された部分文字列または文字を含むかどうかをチェックする
( std::basic_string_view<CharT,Traits> の公開メンバ関数)
2つの文字列を比較する
(公開メンバ関数)
部分文字列を返す
(公開メンバ関数)