std::basic_string_view<CharT,Traits>::ends_with
來自 cppreference.com
< cpp | string | basic string view
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(), ch)。
3) 字尾是空終止字元字串。實際返回 ends_with(basic_string_view(s))。
目錄 |
[編輯] 引數
sv | - | 一個字串檢視,可能是從 std::basic_string 隱式轉換的結果 |
ch | - | 單個字元 |
s | - | 以空字元結尾的字串 |
[編輯] 返回值
如果字串檢視以提供的字尾結尾,則為 true,否則為 false。
[編輯] 註記
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_starts_ends_with |
201711L |
(C++20) | 字串字首和字尾檢查:starts_with() 和 ends_with() |
[編輯] 示例
執行此程式碼
#include <cassert> #include <string_view> int main() { using namespace std::literals; assert ("" // (1) ends_with( basic_string_view sv ) && std::string_view("https://cppreference.tw").ends_with(".com"sv) == true && std::string_view("https://cppreference.tw").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 ); }
[編輯] 參閱
(C++20) |
檢查字串檢視是否以給定字首開頭 (公共成員函式) |
(C++20) |
檢查字串是否以給定字首開頭 ( std::basic_string<CharT,Traits,Allocator> 的公共成員函式) |
(C++20) |
檢查字串是否以給定字尾結尾 ( std::basic_string<CharT,Traits,Allocator> 的公共成員函式) |
(C++23) |
檢查字串是否包含給定子字串或字元 ( std::basic_string<CharT,Traits,Allocator> 的公共成員函式) |
(C++23) |
檢查字串檢視是否包含給定子字串或字元 (公共成員函式) |
比較兩個檢視 (公共成員函式) |