To find out if a specific string exists in a string

Asked 2 years ago, Updated 2 years ago, 116 views

When there is a variable of string type How do I find out if there is a specific string in it?

, for example, "Hello, It's me" , for the "me" true, . , the "???? The" false, such as .

I'd like you to find the index, too.

string c++

2022-09-22 22:26

1 Answers

You must write std::string::find.

int main(){
    string s1 = "hello! C wor";
    string s2 = "world";

    ssize_t pos;
    if ( (pos = s1.find(s2, 0, 3)) != std::string::npos) {
        std::cout << "found!" << '\n';
        cout << pos << endl;
    }

}


2022-09-22 22:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.