C++

C++ String starts with

There comes a time when the programmer has to know what a string starts with. This knowledge can be used to choose or eliminate items in a list of characters. So, a programmer may want to know if a string starts with a particular character or with a particular sub-string. A programmer can write code that will check the initial characters of a string, one-by-one, and compare that with a prefix sub-string. However, all the strategies involved have already been done by the C++ string library.

The C++ string class of the string library has the member function, starts_with(). This does the work for the programmer, but the programmer needs to know how to use the function. And that is why this tutorial is being produced. There are three variants of the string starts_with() member function. Variants of the same function are called overloaded functions.

The basic approach for the start_with() member function is to compare a short independent sub-string with the first short segment of the string in question. If they are the same, then the function returns true. If they are different, the function returns false.

Caution: The starts_with() member function is a C++20 feature. In this article, we are today in 2021, so your compiler may not successfully compile the code samples.

Article Content

bool starts_with(charT x) const

This member function checks if the string literal starts with a particular character. Remember that the string literal has to be of an object instantiated from the string class. The following program checks if the string starts with ‘W’ in uppercase:

#include <iostream>

#include <string>

using namespace std;

   
int main()

{

    basic_string str = "We are moving on.";

     
    bool bl = str.starts_with('W');

    cout <<bl <<endl;

 
    return 0;

}

The output should be 1, for true.

Matching is case sensitive. So the output of the following program should be false:

#include <iostream>

#include <string>

using namespace std;

   
int main()

{

    basic_string<char> str = "We are moving on.";

     
    bool bl = str.starts_with('w');

    cout <<bl <<endl;

 
    return 0;

}

The output should be 0 for false.

A string can also start with a non-alphabetic character. The following program checks if the string starts with ‘[’ :

#include <iostream>

#include <string>

using namespace std;

   
int main()

{

    basic_string<char>str = "[Note: Note information - - -. — end note]";

   
    bool bl = str.starts_with('[');

    cout <<bl <<endl;

 
    return 0;

}

The output should be 1, for true

bool starts_with(const charT* x) const

The programmer may want to know if a string of interest starts with a particular independent sub-string. In this case, he has to use this member function. The following program illustrates this:

#include <iostream>

#include <string>

using namespace std;

   
int main()

{

    basic_string str = "We are moving on.";

    const char* ss = "We are";

     
    bool bl = str.starts_with(ss);

    cout <<bl <<endl;

 
    return 0;

}

The output should be 1 for true.

Matching is case-sensitive. So the output of the following program, where the independent sub-string has more than one character, should be false:

#include <iostream>

#include <string>

using namespace std;

   
int main()

{

    basic_string<char> str = "We are moving on.";

    const char* ss = "WE ARE";

     
    bool bl = str.starts_with(ss);

    cout <<bl <<endl;

 
    return 0;

}

The output should be 0 for false.

A string can also start with non-alphabetic characters. The following program checks if the string starts with a particular sub-string of numbers:

#include <iostream>

#include <string>

using namespace std;

   
int main()

{

    basic_string<char>str = "8762HT is a code number.";

    const char* ss = "8762";

     
    bool bl = str.starts_with(ss);

    cout <<bl <<endl;

 
    return 0;

}

The output should be 1, for true.

bool starts_with(basic_string_view x) const

String View

The argument to the starts_with member function can be a string_view object. The next question is, “What is a string_view?”. A string_view is a range from some original string that becomes the list of a new string object. The characters are not copied from the original string; they are referenced. That is, the elements of the original string are not copied; they are referenced. However, this string-view has many of the functions that the string class has. The string_view is also a class from which string_view objects are created. The following program shows the similarity of a string_view class and a string:

#include <iostream>

#include <string_view>

using namespace std;

   
int main()

{

    const char* str = "Everything that goes up must come down.";

    string_view strV(str, 23);


    for (int i=0; i <strV.size(); i++)

        cout <<strV[i];

    cout <<endl;


    return 0;

}

The output is:

Everything that goes up

The string_view library had to be included. Note that in the declaration, string_view and not basic_string_view have been used. The first 23 characters of the original string became the characters of the string_view. The statement of the program for the construction of the string_view object is:

string_view strV(str, 23);

If a character in the range of the original string is changed, the string view is also changed. The following program illustrates this:

#include <iostream>

#include <string_view>

using namespace std;

   
int main()

{

    char str[] = "Everything that goes up must come down.";

    string_view strV(str, 23);

    str[1] = 'a'; str[2] = 'r';  str[3] = 'l';


    for (int i=0; i <strV.size(); i++)

        cout <<strV[i];

    cout <<endl;


    return 0;

}

The output is:

Earlything that goes up

The first, second, and third characters of the original string were changed after the string_view object had been declared. This confirms that, though the string_view is an object, it references a range in the original string and does not have a copy of the range.

The text of the original string can be made constant. To achieve this, use a const pointer to char instead of an array-of-chars. The following program does not compile, issuing an error message because the text of the original string has been made constant:

#include <iostream>

#include <string_view>

using namespace std;

   
int main()

{

    const char* str = "Everything that goes up must come down.";

    string_view strV(str, 23);

    str[1] = 'a'; str[2] = 'r';  str[3] = 'l';


    for (int i=0; i <strV.size(); i++)

        cout <<strV[i];

    cout <<endl;


    return 0;

}

String View Argument

The syntax for the string_starts() function is:

bool starts_with(basic_string_view<charT, traits>x) const

How to create a string_view object has been illustrated above. After creating the object, pass it as an argument to the starts_with() string member function. The following program illustrates this:

#include <iostream>

#include <string_view>

#include <string>

using namespace std;

   
int main()

{

    const char* str = "Everything that goes up must come down.";

    string_view strV(str, 23);


    const char* ss = "Everything";


    bool bl = str.starts_with(ss);


    cout <<bl <<endl;


    return 0;

}

The output should be true, for 1. This time, the string and string_view libraries have been included.

If the programmer wants to change the original string, he has to use an array-of-chars as an argument to the string_view constructor instead of a constant pointer to char. The following program shows the situation, how the original string will be changing:

#include <iostream>

#include <string_view>

#include <string>

using namespace std;

   
int main()

{

    char str[] = "Everything that goes up must come down.";

    string_view strV(str, 23);

    str[5] = 'a'; str[6] = 'r';  str[7] = 'l'; str[8] = 'r';  str[9] = 'l';


    const char* ss = "Everyapple";


    bool bl = str.starts_with(ss);


    cout <<bl <<endl;


    return 0;

}

The output should be 1, for true.

Even if the argument to the start_with() member function is a string_view object, casing is still respected. The following program illustrates this:

#include <iostream>

#include <string_view>

#include <string>

using namespace std;

   
int main()

{

    char str[] = "Everything that goes up must come down.";

    string_view strV(str, 23);


    const char* ss = "everything";


    bool bl = str.starts_with(ss);


    cout <<bl <<endl;


    return 0;

}

The output should be 0 for false. The first ‘e’ in the sub-string is in lowercase, while the first ‘E’ in the string of interest is in uppercase.

If the argument to the starts_with() member function is a string_view, then non-alphabet characters can still be part of strings. In the following program, a set of consecutive integer characters are checked if they start the string of interest, where the start_with() argument is a string_view:

#include <iostream>

#include <string_view>

#include <string>

using namespace std;

   
int main()

{

    const char* str = "8762HT is a code number.";

    string_view strV(str);


    const char* ss = "8762";


    bool bl = str.starts_with(ss);


    cout <<bl <<endl;


    return 0;

}

The output should be 1 for true.

Conclusion

The string class in C++ has a member function called start_with(). It checks if a sub-independent string, forms the first characters of a string of interest (prefix). The overloaded member functions are starts_with(charT x), starts_with(const charT* x), and starts_with(string_view x). Each returns a bool.

Chrys

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.