Saturday, October 18, 2025
HomeData Modelling & AIstarts_with() and ends_with() in C++20 with Examples

starts_with() and ends_with() in C++20 with Examples

In this article, we will be discussing starts_with() and ends_with() with examples in C++20.

starts_with()

This function efficiently checks if a string begins with the given prefix or not. This function written in both std::basic_string and in std::basic_string_view. 

Syntax:

template <typename PrefixType> bool starts_with(PrefixType prefix)

In the above syntax the prefix can be:

  • string
  • string view
  • single character or C-style string with null-terminated character

starts_with() with different types of Prefix:

bool starts_with(std::basic_string_view<Char T, Traits> x) const noexcept;
bool starts_with(CharT x) const noexcept;
bool starts_with(const CharT *x) const;

All the three overloaded forms of the function effectively return std::basic_string_view<Char T,  Traits>(data(), size()).starts_with(x);

Parameters: It requires a single character sequence or a single character to compare to the start of the string.

Return Value: It returns the boolean true or false indication the following:

  • True: If string starts with the prefix.
  • False: If string does not start with the prefix.

Program 1:

Below program to demonstrates the concept of starts_with() in C++:

C++




// C++ program to illustrate the use
// of starts_with()
#include <iostream>
#include <string>
#include <string_view>
using namespace std;
  
// Function template to check if the
// given string starts with the given
// prefix or not
template <typename PrefixType>
void if_prefix(const std::string& str,
               PrefixType prefix)
{
    cout << "'" << str << "' starts with '"
         << prefix << "': "
         << str.starts_with(prefix)
         << endl;
}
  
// Driver Code
int main()
{
    string str = { "neveropen" };
  
    if_prefix(str, string("geek"));
  
    // prefix is string
    if_prefix(str, string_view("geek"));
  
    // prefix is string view
    if_prefix(str, 'g');
  
    // prefix is single character
    if_prefix(str, "geek\0");
  
    // prefix is C-style string
    if_prefix(str, string("for"));
  
    if_prefix(str, string("Geek"));
  
    if_prefix(str, 'x');
  
    return 0;
}


Output:

This function efficiently checks if a string ends with the given suffix or not. This function written in both std::basic_string and in std::basic_string_view.

Syntax:

template <typename SuffixType> bool starts_with(SuffixType suffix)

In the above syntax the suffix can be:

  • string
  • string view
  • single character or C-style string with null-terminated character.

ends with() different types of Suffix:

constexpr bool ends_with(std::basic_string_view<Char T, Traits> sv) const noexcept;|
constexpr boll ends_with(CharT c) const noexcept;
constexpr bool ends_with(const CharT* s) const;

All the three overloaded forms of the function effectively return std::basic_string_view<Char T,  Traits>(data(), size()).ends_with(x);

Parameters: It requires a single character sequence or a single character to compare to the end of the string.

Return Value: It returns the boolean true or false indicating the following:

  • True: If string ends with the suffix.
  • False: If string does not end with the suffix.

Program 2:

Below program to demonstrates the concept of ends_with() in C++:

C++




// C++ program to illustrate the use
// of ends_with()
#include <iostream>
#include <string>
#include <string_view>
using namespace std;
  
// Function template to check if the
// given string ends_with given string
template <typename SuffixType>
void if_suffix(const std::string& str,
               SuffixType suffix)
{
    cout << "'" << str << "' ends with '" << suffix << "': " << str.ends_with(suffix) << std::endl;
}
  
// Driver Code
int main()
{
    string str = { "neveropen" };
  
    if_suffix(str, string("neveropen"));
  
    // suffix is string
    if_suffix(str, string_view("neveropen"));
  
    // suffix is string view
    if_suffix(str, 's');
  
    // suffix is single character
    if_suffix(str,
              "neveropen\0");
  
    // suffix is C-style string
    if_suffix(str, string("for"));
  
    if_suffix(str, string("Geeks"));
  
    if_suffix(str, 'x');
  
    if_suffix(str, '\0');
}


Output:

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS