std::basic_string_view<CharT,Traits>::ends_with
来自 cppreference.cn
                    
                                        
                    < 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.cn").ends_with(".com"sv) == true && std::string_view("https://cppreference.cn").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) | 检查字符串视图是否包含给定子字符串或字符 (公共成员函数) | 
| 比较两个视图 (公共成员函数) | 


