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(), ch)。
3) 字首是一個以 null 結尾的字元字串。實際返回 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.tw"sv.starts_with("http"sv) == true && "https://cppreference.tw"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) |
檢查字串檢視是否包含給定子字串或字元 (公共成員函式) |
比較兩個檢視 (公共成員函式) |