std::basic_string<CharT,Traits,Allocator>::starts_with - cppreference.com
提供: cppreference.com
<tbody> </tbody>
|
|
(1) | (C++20以上) |
|
|
(2) | (C++20以上) |
|
|
(3) | (C++20以上) |
文字列が指定された接頭辞で始まるかどうか調べます。 接頭辞は以下のいずれかです。
1) 文字列ビュー sv。 別の std::basic_string から暗黙に変換された結果でも構いません。
2) 単一の文字 c。
3) ヌル終端文字列 s。
3つのオーバーロードはすべて実質的に std::basic_string_view<CharT, Traits>(data(), size()).starts_with(x); を返します (ただし x は引数です)。
引数
| sv | - | 文字列ビュー。 別の std::basic_string から暗黙に変換された結果でも構いません。
|
| c | - | 単一の文字。 |
| s | - | ヌル終端文字列。 |
戻り値
文字列が指定された接頭辞で始まるならば true、そうでなければ false。
例
#include <iostream> #include <string_view> #include <string> template <typename PrefixType> void test_prefix_print(const std::string& str, PrefixType prefix) { std::cout << '\'' << str << "' starts with '" << prefix << "': " << str.starts_with(prefix) << '\n'; } int main() { std::boolalpha(std::cout); auto helloWorld = std::string("hello world"); test_prefix_print(helloWorld, std::string_view("hello")); test_prefix_print(helloWorld, std::string_view("goodbye")); test_prefix_print(helloWorld, 'h'); test_prefix_print(helloWorld, 'x'); }
出力:
'hello world' starts with 'hello': true 'hello world' starts with 'goodbye': false 'hello world' starts with 'h': true 'hello world' starts with 'x': false