std::basic_string_view<CharT,Traits>::starts_with

Checks if the string view begins with the given prefix, where

# Declarations

constexpr bool starts_with( basic_string_view sv ) const noexcept;

(since C++20)

constexpr bool starts_with( CharT ch ) const noexcept;

(since C++20)

constexpr bool starts_with( const CharT* s ) const;

(since C++20)

# Parameters

# Return value

true if the string view begins with the provided prefix, false otherwise.

# Notes

Feature-test macro Value Std Feature __cpp_lib_starts_ends_with 201711L (C++20) String prefix and suffix checking: starts_with() and ends_with()

# Example

#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
    );
}

# See also