Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 = { "geeksforgeeks" };
  
    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 = { "geeksforgeeks" };
  
    if_suffix(str, string("geeks"));
  
    // suffix is string
    if_suffix(str, string_view("geeks"));
  
    // suffix is string view
    if_suffix(str, 's');
  
    // suffix is single character
    if_suffix(str,
              "geeks\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:



Last Updated : 28 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads