70

I have a text file that I want to read. I want to know if one of the lines contains [ so I tried :

if(array[i] == "[")

But this isn't working.

How can I check if a string contains a certain character?

6

5 Answers 5

126

Look at the documentation string::find

std::string s = "hell[o";
if (s.find('[') != std::string::npos)
    ; // found
else
    ; // not found
2
  • 1
    What is npos? Is that the position of the string that you are trying to match the character with? Nov 17, 2020 at 19:59
  • 6
    @AdanVivero, if you read the Return value part of the documentation, npos is the value returned if no such substring is found.
    – thibsc
    Nov 18, 2020 at 5:56
32

Starting from C++23 you can use std::string::contains

#include <string>

const auto test = std::string("test");

if (test.contains('s'))
{
    // found!
}
0
9

I did it in this way.

string s = "More+";

if(s.find('+')<s.length()){ //to find +
    //found
} else {
    //not found
}

It works even if you want to find more than one character but they should be lined up together. Be sure to replace '' with "":

string s = "More++";

if(s.find("++")<s.length()){ //to find ++
    //found
} else {
    //not found
}
1
3

In strings, we can use find() to get the first occurrence (position) of the given "string"

string s = "dumm[y[";
int found = s.find('[');

cout<<"$ is present at position "<<firstOccurrence;   //$ is present at position 4

if (found < str.length()) {
    // char found
}
else{
    // char not found
}
2
  • 1
    This code does not compile! Even if you'd replace firstOccurrence by found and str by s, find() would return 4, not 3. Please at least run your code and make sure it is correct before posting an answer on here, thank you! Aug 23, 2020 at 11:10
  • 1
    @SebastianWilke thanks for your update. Edited the mistake I did. ill check my code before posting and won't repeat it again.! Aug 24, 2020 at 18:23
0

Use find() method, but remember find() gives the position!

string str;
char letter, entered_char;

cout<<"Enter  a string: ";
cin>>str;

cout<<"Enter character to be found: ";
cin>>entered_char;

//remember: find() gives the position of char
letter = str.find(entered_char);
//'letter' variable contains the position of entered_char

//if entered character is not equal to str[position found] 
if(entered_char != str[letter]){
    cout<<"Not found!";
} else {
    cout<<"Found";
}
2
  • Why use char as type of letter index? Usually size_t is used for sizes and indexes. In particular, here that would avoid narrowing conversion to char which would result in wrong value if returned index is not representable in char range.
    – YurkoFlisk
    Jul 25, 2022 at 13:04
  • And even if you used size_t, if the character is not found, your code would invoke undefined behaviour since string::find returns string::npos and you use it to index the string in the if statement using string::operator[], which is defined behaviour only if its index is between 0 and string length (see documentation for details). You should instead just check whether the index is npos as in the accepted answer.
    – YurkoFlisk
    Jul 25, 2022 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.