68

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Robert Lewis
  • 701
  • 1
  • 5
  • 3

5 Answers5

122

Look at the documentation string::find

std::string s = "hell[o";
if (s.find('[') != std::string::npos)
    ; // found
else
    ; // not found
TrebledJ
  • 8,713
  • 7
  • 26
  • 48
thibsc
  • 3,747
  • 2
  • 18
  • 38
  • 1
    What is npos? Is that the position of the string that you are trying to match the character with? – Adan Vivero Nov 17 '20 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 '20 at 05:56
27

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

#include <string>

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

if (test.contains('s'))
{
    // found!
}
Synck
  • 2,727
  • 22
  • 20
8

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
}
Gourav
  • 2,746
  • 5
  • 28
  • 45
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
}
  • 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! – SebastianWilke Aug 23 '20 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.! – Akash Srinivasan Aug 24 '20 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";
}
Gaurav
  • 1
  • 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 '22 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](https://en.cppreference.com/w/cpp/string/basic_string/operator_at) for details). You should instead just check whether the index is `npos` as in the accepted answer. – YurkoFlisk Jul 25 '22 at 13:06