命名空间
版本
操作

std::basic_string<CharT,Traits,Allocator>::starts_with

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
constexpr bool
    starts_with( std::basic_string_view<CharT,Traits> sv ) const noexcept;
(1) (since C++20)
constexpr bool
    starts_with( CharT ch ) const noexcept;
(2) (since C++20)
constexpr bool
    starts_with( const CharT* s ) const;
(3) (since C++20)

检查字符串是否以给定的前缀开始。前缀可以是以下之一

1) 字符串视图 sv (可能是从另一个 std::basic_string 隐式转换的结果)。
2) 单个字符 ch
3) 空终止字符字符串 s

所有三个重载实际上都返回 std::basic_string_view<CharT, Traits>(data(), size()).starts_with(x),其中 x 是参数。

目录

[编辑] 参数

sv - 字符串视图,可能是从另一个 std::basic_string 隐式转换的结果
ch - 单个字符
s - 空终止字符字符串

[编辑] 返回值

true 如果字符串以提供的前缀开始,则为 false 否则为否。

[编辑] 注意事项

特性测试 Std 特性
__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) implicit conversion string to string_view
        && !str.starts_with("he"s)  // (1) implicit conversion string to string_view
        && str.starts_with('H')     // (2)
        && !str.starts_with('h')    // (2)
        && str.starts_with("He")    // (3)
        && !str.starts_with("he")   // (3)
    );
}

[编辑] 参见

(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> 的公共成员函数) [编辑]
比较两个字符串
(公共成员函数) [编辑]
返回一个子字符串
(公共成员函数) [编辑]