std::basic_string_view<CharT,Traits>::starts_with
来自cppreference.com
< 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(), c)。
3) 前缀为空终止字符串。相当于返回 starts_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) starts_with( basic_string_view ) && "https://cppreference.com"sv.starts_with("http"sv) == true && "https://cppreference.com"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) |
检查字符串视图是否含有给定的子串或字符 (公开成员函数) |
比较两个视图 (公开成员函数) |