CPP : Check if a String Starts with an Another given String

The C++ string class internally stores characters in a char array, but all memory management, allocation, and null termination is handled by the string class itself, which is why it is simple to use. The length of a C++ string can be changed at runtime due to dynamic memory allocation, which is similar to vectors. Because the string class is a container class, we can iterate over all of its characters with an iterator, just like we can with vectors, sets, and maps, but we usually use a simple for loop to iterate over the characters and index them with the [] operator.

The std::string class in C++ lacks a startsWith() function for determining whether a string begins with another string. Let’s look at how to do it with std::string::find and the Boost Library.

Examples:

1)Case sensitive

Input:

string mainstring = "This is BTechGeeks"

 string matchstring = "this"

Output:

Do not start with given string

2)Case insensitive

Input:

string mainstring = "This is BTechGeeks"

 string matchstring = "this"

Output:

starts with given string

Check if a String Starts with an Another given String

There are several methods to check string starts with an another given string some of them are:

Method #1:Using find() function(case sensitive)

The find() member function of the std::string class accepts a string and searches for the first occurrence of that string in the associated string object. If the string matches, it returns the position of the matched string; otherwise, it returns std::string::npos.

To find the first occurrence of a given string, let’s use std:string::find in the startsWith() implementation. If the returned position is 0, it ensures that our main string begins with the given string.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "this";
    // using find() function
    if (mainstring.find(matchstring) == 0)
        cout << "starts with given string" << endl;
    else
        cout << "Do not start with given string" << endl;
}

Output:

Do not start with given string

Method #2:Using find() function(case insensitive)

To implement startsWith() in a case-insensitive manner, first convert both to lower case and then use std::string::find to find the position of the given string.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "this";
    // Converting mainstring to lower_case
    transform(mainstring.begin(), mainstring.end(),
              mainstring.begin(), ::tolower);
    // Converting matchstring to lower_case
    transform(matchstring.begin(), matchstring.end(),
              matchstring.begin(), ::tolower);
    // using find() function
    if (mainstring.find(matchstring) == 0)
        cout << "starts with given string" << endl;
    else
        cout << "Do not start with given string" << endl;
}

Output:

starts with given string

Method #3:Using boost library(case sensitive)

The algorithm library in Boost supports both case sensitive and case insensitive implementations of the startsWith() function for strings.

Case sensitive version:

boost::algorithm::starts_with(mainstring, matchstring) ;

Header file :<boost/algorithm/string.hpp>

Below is the implementation:

#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "this";
    // using boost function
    if (boost::algorithm::starts_with(mainstring,
                                      matchstring))
        cout << "starts with given string" << endl;
    else
        cout << "Do not start with given string" << endl;
}

Output:

Do not start with given string

Method #4:Using boost library(case insensitive)

Syntax:

boost::algorithm::istarts_with(mainString, toMatchString) ;

Below is the implementation:

#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
    // given main string
    string mainstring = "This is BTechGeeks";
    // given to match string
    string matchstring = "this";
    // using boost function
    if (boost::algorithm::istarts_with(mainstring,
                                       matchstring))
        cout << "starts with given string" << endl;
    else
        cout << "Do not start with given string" << endl;
}

Output:

starts with given string

Related Programs: