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

Checks if the string begins with the given prefix. The prefix may be one of the following:

# Declarations

constexpr bool
starts_with( std::basic_string_view<CharT,Traits> 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 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>
#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)
    );
}

# See also