Q:

How to search in a string within a given range?

I have to search a String for a specific character, within a specified range. The code I made is as follows:
str.erase(str.begin(), str.begin()+5);
str.erase(str.begin() + 9, str.end()); // The range to be searched has been specified ie 5th index to 9th index
 if(  str.find('a') != string::npos){
       cout << "YES, it exists" << endl;
   }else{
       cout << "No, it doesnt" << endl;
   }

The above code works as expected, but I would love to know if there is a standard library function that would do this work.(Probably in one line)

A:

... but I would love to know if there is a standard library function that would do this work.(Probably in one line)

You can use std::find() to do this without changing the original string variable:
if(std::find(std::begin(str) + 5, std::end(str) - 9,'a') != std::end(str) - 9){
   cout << "YES, it exists" << endl;
}else{
   cout << "No, it doesnt" << endl;
}

