std::basic_string_view<CharT,Traits>::ends_with

出自cppreference.com
 
 
 
 
constexpr bool ends_with( basic_string_view 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) 後綴為字符串視圖。相當於返回 size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0
2) 後綴為���個字符。相當於返回 !empty() && Traits::eq(back(), c)
3) 後綴為空終止字符串。相當於返回 ends_with(basic_string_view(s))

參數

sv - 可能為來自 std::basic_string 轉換結果的字符串視圖
ch - 單個字符
s - 空終止字符串

返回值

若字符串視圖終於給定後綴則為 true,否則為 false

示例

#include <cassert>
#include <string_view>

int main()
{
    using namespace std::literals;

    assert
    (""
        // (1) ends_with( basic_string_view sv )
        && std::string_view("https://cppreference.com").ends_with(".com"sv) == true
        && std::string_view("https://cppreference.com").ends_with(".org"sv) == false

        // (2) ends_with( CharT c )
        && std::string_view("C++20").ends_with('0') == true
        && std::string_view("C++20").ends_with('3') == false

        // (3) ends_with( const CharT* s )
        && std::string_view("string_view").ends_with("view") == true
        && std::string_view("string_view").ends_with("View") == false
    );
}

參閱

檢查字符串視圖是否始於給定前綴
(公開成員函數) [編輯]
檢查字符串是否始於給定前綴
(std::basic_string<CharT,Traits,Allocator> 的公開成員函數) [編輯]
(C++20)
檢查字符串是否終於給定後綴
(std::basic_string<CharT,Traits,Allocator> 的公開成員函數) [編輯]
(C++23)
檢查字符串是否含有給定的子串或字符
(std::basic_string<CharT,Traits,Allocator> 的公開成員函數) [編輯]
(C++23)
檢查字符串視圖是否含有給定的子串或字符
(公開成員函數) [編輯]
比較兩個視圖
(公開成員函數) [編輯]