I want to know the positions of the "_" in a string:
string str("BLA_BLABLA_BLA.txt");
Something like:
string::iterator it;
for ( it=str.begin() ; it < str.end(); it++ ){
if (*it == "_") //this goes wrong: pointer and integer comparison
{
pos(1) = it;
}
cout << *it << endl;
}
Thanks, André
Note that "_"
is a string literal, while '_'
is a character literal.
If you dereference an iterator into a string, what you get is a character. Of course, characters can only be compared to character literals, not to string literals.
However, as others have already noticed, you shouldn't implement such an algorithm yourself. It's been done a million times, two of which (std::string::find()
and std::find()
) ended up in C++' standard library. Use one of those.
std::find(str.begin(), str.end(), '_');
// ^Single quote!
string::find is your friend. http://www.cplusplus.com/reference/string/string/find/
someString.find('_');
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