C++ boost::algorithm::string::starts_with()














































C++ boost::algorithm::string::starts_with()



starts_with():

     This function is included in the "boost/algorithm/string" library. This library contains some brilliant methods which  help in accomplishing string manipulations that are lacking in STL library. 

This function is used to check whether the start of a given string is same as the given pattern, i.e., checks whether the test string(pattern) is the prefix of the given input string. In other words it checks if the input begins with the test string or not.

header file:

boost/algorithm/string.hpp

syntax:

     starts_with(input,test);

parameters:

    input : an input string(sequence)

    test : a test string(sequence)

The given function is case sensitive, i.e., it does the check case sensitively. For example, "Apple" is not same as "apple"

 There is another variation present i.e., istarts_with() , which takes the same parameters as input, but instead of doing a case sensitive test, it ignores the case of the characters and does the check, i.e., it does the check case insensitively. For example, "Apple" is same as "apple".

SAMPLE CODE:

#include<iostream> #include<boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; int main() { //Input strings string str[] = {"Welcome","You are using boost"," ITs very interesting","IT is very effective"}; //Test strings string test[] = {"Wel","you "," IT","it is"}; //performing the checks and printing the results with starts_with cout<<"Using starts_with()"<<endl<<endl; for(int i=0;i<4;i++) { cout<<str[i]<<" is checked with "<<test[i]<<endl; cout<<"Result : The string starts with test- "<<starts_with(str[i],test[i])<<endl<<endl; } //performing the checks and printing the results with istarts_with cout<<"Using istarts_with()"<<endl<<endl; cout<<str[3]<<" is checked with "<<test[3]<<endl; cout<<"Result : The string starts with test- "<<istarts_with(str[3],test[3])<<endl; }

OUTPUT:

EXPLANATION:

In the above output, 1 means TRUE and 0 means FALSE.

As we can observe for str1 and str3 starts_with() returns TRUE and for str2 and atr4 it returns FALSE as the test string is "you" and "it is" instead of "You" and "IT is" respectively. (starts_with() does case sensitive check).

On the contrary, istarts_with() return TRUE for str4 as it does case insensitive check.



More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 691 0
C++ boost::algorithm::string::split() 833 0
C++ boost::algorithm::string::find_all() 747 0
C++ boost::algorithm::string::erase_tail() 270 0
C++ boost::algorithm::string::replace_tail() 229 0
C++ boost::algorithm::string::erase_head() 303 0
C++ boost::algorithm::string::replace_head() 232 0
C++ boost::algorithm::string::erase_all() 1049 1
C++ boost::algorithm::replace_all() 2455 0
C++ boost::algorithm::string::erase_nth() 232 0
C++ boost::algorithm::string::replace_nth() 238 0
C++ boost::algorithm::string::replace_last() 279 0
C++ boost::algorithm::string::erase_last() 271 0
C++ boost::algorithm::string::erase_first() 238 1
C++ boost::algorithm::string::replace_first() 492 0
C++ boost::algorithm::string::find_token() 413 0
C++ boost::algorithm::string::find_tail() 217 1
C++ boost::algorithm::string::find_head() 231 0
C++ boost::algorithm::string::find_last() 341 1
C++ boost::algorithm::string::find_first() 848 1
C++ boost::algorithm::string::all() 256 1
C++ boost::algorithm::string::lexicographical_compare() 234 0
C++ boost::algorithm::string::equals() 479 0
C++ boost::algorithm::string::contains() 2117 0
C++ boost::algorithm::string::ends_with() 1489 0
C++ boost::algorithm::string::starts_with() 2388 0
C++ boost::algorithm::string::trim_if() 797 1
C++ boost::algorithm::string::trim() 4511 0
C++ boost::algorithm::string::trim_right_if() 581 1
C++ boost::algorithm::string::trim_left_if() 447 1
C++ boost::algorithm::string::trim_right() 348 3
C++ boost::algorithm::string::trim_left() 419 1
C++ boost::algorithm::string::to_lower() 1076 1
C++ boost::algorithm::string::to_upper() 404 1
C++ Program to Implement Dequeue 1620 5
C++ Program to Implement Dequeue 263 3

Comments