std::basic_string_view<CharT,Traits>::starts_with
来自 cppreference.cn
< cpp | string | basic string view
constexpr bool starts_with( basic_string_view sv ) const noexcept; |
(1) | (自 C++20 起) |
constexpr bool starts_with( CharT ch ) const noexcept; |
(2) | (自 C++20 起) |
constexpr bool starts_with( const CharT* s ) const; |
(3) | (自 C++20 起) |
检查字符串视图是否以给定前缀开头,其中
1) 前缀是字符串视图。 有效地返回 basic_string_view(data(), std::min(size(), sv.size())) == sv。
2) 前缀是单个字符。 有效地返回 !empty() && Traits::eq(front(), ch)。
3) 前缀是空字符结尾的字符串。 有效地返回 starts_with(basic_string_view(s))。
内容 |
[编辑] 参数
sv | - | 字符串视图,可能是从 std::basic_string 隐式转换的结果 |
ch | - | 单个字符 |
s | - | 空字符结尾的字符串 |
[编辑] 返回值
true 如果字符串视图以提供的前缀开头,则为 false 否则为 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) starts_with( basic_string_view ) && "https://cppreference.cn"sv.starts_with("http"sv) == true && "https://cppreference.cn"sv.starts_with("ftp"sv) == false // (2) starts_with( CharT ) && "C++20"sv.starts_with('C') == true && "C++20"sv.starts_with('J') == false // (3) starts_with( const CharT* ) && std::string_view("string_view").starts_with("string") == true && std::string_view("string_view").starts_with("String") == 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) |
检查字符串视图是否包含给定的子字符串或字符 (公共成员函数) |
比较两个视图 (公共成员函数) |