溫馨提示×

c++中怎么判斷string包含某個字符串

c++
小億
1794
2024-03-01 17:35:19
欄目: 編程語言

在C++中,可以使用find函數來判斷一個字符串是否包含另一個字符串。find函數返回被查找的字符串在主字符串中第一次出現的位置,如果找不到,則返回string::npos。

示例代碼如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    std::string substr = "Hello";

    if (str.find(substr) != std::string::npos) {
        std::cout << "字符串包含子字符串" << std::endl;
    } else {
        std::cout << "字符串不包含子字符串" << std::endl;
    }

    return 0;
}

在這個示例中,我們判斷了字符串str是否包含子字符串substr,如果包含則輸出字符串包含子字符串,否則輸出字符串不包含子字符串。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女