std::basic_string<CharT,Traits,Allocator>::ends_with
来自cppreference.com
< cpp | string | basic string
| bool ends_with(std::basic_string_view<CharT, Traits> x) const noexcept; |
(1) | (C++20 起) |
| bool ends_with(CharT x) const noexcept; |
(2) | (C++20 起) |
| bool ends_with(const CharT* x) const; |
(3) | (C++20 起) |
检查 string 是否终于给定后缀,其中
1) 后缀为 string_view (可以是从另一
std::basic_string 隐式转换的结果)2) 后缀为单个字符
3) 后缀为 C 风格字符串
所有三个重载等效地返回 std::basic_string_view<CharT, Traits>(data(), size()).ends_with(x) 。
目录 |
[编辑] 参数
| x | - | 要与 string 末尾比较的字符序列或单个字符 |
[编辑] 返回值
若 string 终于给定后缀则为 true ,否则为 false 。
[编辑] 示例
运行此代码
#include <iostream> #include <string_view> #include <string> template <typename SuffixType> void test_suffix_print(const std::string& str, SuffixType suffix) { std::cout << '\'' << str << "' ends with '" << suffix << "': " << str.ends_with(suffix) << '\n'; } int main() { std::boolalpha(std::cout); auto helloWorld = std::string("hello world"); test_suffix_print(helloWorld, std::string_view("world")); test_suffix_print(helloWorld, std::string_view("goodby")); test_suffix_print(helloWorld, 'd'); test_suffix_print(helloWorld, 'x'); }
输出:
'hello world' ends with 'world': true 'hello world' ends with 'goodby': false 'hello world' ends with 'd': true 'hello world' ends with 'x': false
[编辑] 参阅
| (C++20) |
检查 string 是否始于给定前缀 (公开成员函数) |
| (C++20) |
检查 string_view 是否始于给定前缀 ( std::basic_string_view<CharT,Traits> 的公开成员函数)
|
| (C++20) |
检查 string_view 是否终于给定后缀 ( std::basic_string_view<CharT,Traits> 的公开成员函数)
|
| 比较二个字符串 (公开成员函数) | |
| 返回子串 (公开成员函数) |

