...

/

String prefix and suffix checking

String prefix and suffix checking

Understand the use of 'starts_with' and 'ends_with' functions with examples.

We'll cover the following...

std::string gets new member functions starts_with and ends_with. They allow you to check if a std::string starts or ends with a specified substring.

Press + to interact
#include <iostream>
#include <string_view>
#include <string>
template <typename PrefixType>
void startsWith(const std::string& str, PrefixType prefix) {
std::cout << " starts with " << prefix << ": " <<
str.starts_with(prefix) << '\n';
}
template <typename SuffixType>
void endsWith(const std::string& str, SuffixType suffix) {
std::cout << " ends with " << suffix << ": " <<
str.ends_with(suffix) << '\n';
}
int main() {
std::cout << std::endl;
std::cout << std::boolalpha;
std::string helloWorld("Hello World");
std::cout << helloWorld << std::endl;
startsWith(helloWorld, helloWorld);
startsWith(helloWorld, std::string_view("Hello"));
startsWith(helloWorld, 'H');
std::cout << "\n\n";
std::cout << helloWorld << std::endl;
endsWith(helloWorld, helloWorld);
endsWith(helloWorld, std::string_view("World"));
endsWith(helloWorld, 'd');
}

Both member functions starts_with and ends_with are predicates ...