std::basic_string<CharT,Traits,Allocator>::ends_with
来自 cppreference.cn
< 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) |