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) 單個字元 c。
3) 空終止字元串 s。
所有三個重載都相當於返回 std::basic_string_view<CharT, Traits>(data(), size()).ends_with(x),其中 x
是形參。
目錄 |
[編輯] 參數
sv | - | 字元串視圖,可為從另一 std::basic_string 隱式轉換的結果
|
c | - | 單個字元 |
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) ); }
[編輯] 參閱
(C++20) |
檢查字元串是否始於給定前綴 (公開成員函數) |
(C++20) |
檢查字元串視圖是否始於給定前綴 ( std::basic_string_view<CharT,Traits> 的公開成員函數)
|
(C++20) |
檢查字元串視圖是否終於給定後綴 ( std::basic_string_view<CharT,Traits> 的公開成員函數)
|
(C++23) |
檢查字元串是否含有給定的子串或字元 (公開成員函數) |
(C++23) |
檢查字元串視圖是否含有給定的子串或字元 ( std::basic_string_view<CharT,Traits> 的公開成員函數)
|
比較兩個字元串 (公開成員函數) | |
返回子串 (公開成員函數) |