In this article we will discuss both case sensitive and insensitive implementations to check if a string starts with an another given string.
In c++, std::string class does not provides any startsWith() function to check if a string starts with an another given string. Let’s see how to do that using std::string::find and Boost Library.
std::string startWith() Implementation using std::string::find
std::string class provides a member function find(), that accepts a string and search for first occurrence of that in the associated string object. If string matches then it returns the position of matched string else return std::string::npos.
For startsWith() implementation let’s use std:string::find to find the first occurrence of given string. If returned position is 0, then it will make sure that our main string starts with the given string.
Case Sensitive Implementation of startsWith() function using std::find
/* * Case Sensitive Implementation of startsWith() * It checks if the string 'mainStr' starts with given string 'toMatch' */ bool startsWith(std::string mainStr, std::string toMatch) { // std::string::find returns 0 if toMatch is found at starting if(mainStr.find(toMatch) == 0) return true; else return false; }
Case Insensitive Implementation of startsWith() function using std::find
For case insensitive implementation of startsWith(), first convert both to lower case and then use std::string::find to check the position of given string.
/* * Case Insensitive Implementation of startsWith() * It checks if the string 'mainStr' starts with given string 'toMatch' */ bool startsWithCaseInsensitive(std::string mainStr, std::string toMatch) { // Convert mainStr to lower case std::transform(mainStr.begin(), mainStr.end(), mainStr.begin(), ::tolower); // Convert toMatch to lower case std::transform(toMatch.begin(), toMatch.end(), toMatch.begin(), ::tolower); if(mainStr.find(toMatch) == 0) return true; else return false; }
Check complete example as follows,
Frequently Asked:
#include <iostream> #include <string> #include <algorithm> /* * Case Sensitive Implementation of startsWith() * It checks if the string 'mainStr' starts with given string 'toMatch' */ bool startsWith(std::string mainStr, std::string toMatch) { // std::string::find returns 0 if toMatch is found at starting if(mainStr.find(toMatch) == 0) return true; else return false; } /* * Case Insensitive Implementation of startsWith() * It checks if the string 'mainStr' starts with given string 'toMatch' */ bool startsWithCaseInsensitive(std::string mainStr, std::string toMatch) { // Convert mainStr to lower case std::transform(mainStr.begin(), mainStr.end(), mainStr.begin(), ::tolower); // Convert toMatch to lower case std::transform(toMatch.begin(), toMatch.end(), toMatch.begin(), ::tolower); if(mainStr.find(toMatch) == 0) return true; else return false; } int main() { std::string mainStr = "This is the sample string"; std::string toMatch = "This"; // Test case-sensitive implementation of startsWith function bool result = startsWith(mainStr , toMatch); std::cout<<result<<std::endl; // Test case-insensitive implementation of startsWith function result = startsWithCaseInsensitive(toMatch, "this"); std::cout<<result<<std::endl; }
Output:
1 1
std::string startWith() using Boost Library
Boost’s algorithm library provides implementation of both case sensitive and insensitive implementation of startsWith() function for string i.e.
case Sensitive Version :
boost::algorithm::starts_with(mainString, toMatchString) ;
case Insensitive Version :
boost::algorithm::istarts_with(mainString, toMatchString) ;
Header File required : <boost/algorithm/string.hpp>
Checkout complete example as follows,
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> int main() { std::string mainStr = "This is the sample string"; std::string toMatch = "This"; // Test case-sensitive implementation of startsWith function bool result = boost::algorithm::starts_with(mainStr , toMatch); std::cout<<result<<std::endl; // Test case-insensitive implementation of startsWith function result = boost::algorithm::istarts_with(mainStr, "this"); std::cout<<result<<std::endl; }
Output:
Pointers in C/C++ [Full Course]
1 1
Good examples. Thanks. Keep posting.
Pingback: C++ : How to check if a String Ends With an another given String – thisPointer.com