std::basic_string<CharT,Traits,Allocator>:: starts_with
|
constexpr
bool
starts_with ( std:: basic_string_view < CharT,Traits > 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 起) |
检查字符串是否以给定前缀开头。前缀可以是以下之一:
sv
(可能是通过隐式转换从另一个
std::basic_string
得到的结果)。
所有三个重载实际上都返回
std::
basic_string_view
<
CharT, Traits
>
(
data
(
)
, size
(
)
)
.
starts_with
(
x
)
,其中
x
是参数。
目录 |
参数
| sv | - |
一个字符串视图,可能由其他
std::basic_string
隐式转换而来
|
| ch | - | 单个字符 |
| s | - | 以空字符结尾的字符串 |
返回值
true
如果字符串以提供的
prefix
开头,
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.starts_with("He"sv) // (1) && !str.starts_with("he"sv) // (1) && str.starts_with("He"s) // (1) 隐式转换 string 为 string_view && !str.starts_with("he"s) // (1) 隐式转换 string 为 string_view && str.starts_with('H') // (2) && !str.starts_with('h') // (2) && str.starts_with("He") // (3) && !str.starts_with("he") // (3) ); }
参见
|
(C++20)
|
检查字符串是否以指定后缀结尾
(公开成员函数) |
|
(C++20)
|
检查字符串视图是否以指定前缀开头
(
std::basic_string_view<CharT,Traits>
的公开成员函数)
|
|
(C++20)
|
检查字符串视图是否以指定后缀结尾
(
std::basic_string_view<CharT,Traits>
的公开成员函数)
|
|
(C++23)
|
检查字符串是否包含指定子串或字符
(公开成员函数) |
|
(C++23)
|
检查字符串视图是否包含指定子串或字符
(
std::basic_string_view<CharT,Traits>
的公开成员函数)
|
|
比较两个字符串
(公开成员函数) |
|
|
返回子字符串
(公开成员函数) |