Good, I would like to know when the first character of a string is "#" and I do not know what function to use. Can somebody help me?
Good, I would like to know when the first character of a string is "#" and I do not know what function to use. Can somebody help me?
A string of characters is nothing more than an array of characters (the class std::string
encapsulates this same complexity). So, and taking into account that the indexes in C ++ start at 0, you have a wide range of possibilities to choose from:
std::string cadena = "ABCDEF";
std::cout << cadena[0] // (1)
<< cadena.at(0) // (2)
<< cadena.front() // (3)
<< *cadena.begin() // (4)
<< *std::begin(cadena); // (5)
at
is similar to the index operator. Check that the index is valid and if it does not throw an exception So to know if the first character is '#'
it is enough to apply the comparison operator to one of the options we have just seen:
if( cadena.front() == '#' )
{
// ...
}