I have a const char* variable and I want to check if it begins with a certain string.
For example:
string sentence = "Hello, world!";
string other = "Hello";
const char* c = sentence.c_str();
if(/*"c" begins with "other"*/)
{
//Do something
}
How can I do this using the if statement?
To check whether a C string begins with a certain substring, you could use strncmp().
For C++ strings, there is a std::string::compare() overload that accepts offsets and lengths.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With