std::basic_string<CharT,Traits,Allocator>::ends_with
來自 cppreference.com
< cpp | string | 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。
所有三個過載實際上都返回 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) 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) ); }
[編輯] 另請參閱
(C++20) |
檢查字串是否以給定字首開頭 (public member function) |
(C++20) |
檢查字串檢視是否以給定字首開頭 ( std::basic_string_view<CharT,Traits> 的公共成員函式) |
(C++20) |
檢查字串檢視是否以給定字尾結尾 ( std::basic_string_view<CharT,Traits> 的公共成員函式) |
(C++23) |
檢查字串是否包含給定子字串或字元 (public member function) |
(C++23) |
檢查字串檢視是否包含給定子字串或字元 ( std::basic_string_view<CharT,Traits> 的公共成員函式) |
比較兩個字串 (public member function) | |
返回子字串 (public member function) |