zoukankan      html  css  js  c++  java
  • C/C++判断字符串是否包含某个子字符串

    C风格

    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    int main()
    {
        string a="abcdefghigklmn";
        char *b="def";
        char *c="123";
         
        if(strstr(a.c_str(), b) == NULL)//在a中查找b,如果不存在,
            cout << "not found ";//输出结果。
        else//否则存在。
            cout <<"found "//输出结果。
        if(strstr(a.c_str(), c) == NULL)//在a中查找b,如果不存在,
            cout << "not found ";//输出结果。
        else//否则存在。
            cout <<"found "//输出结果。
        return 0;
    }
     
    C++风格
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string a="abcdefghigklmn";
        string b="def";
        string c="123";
        string::size_type idx;
         
        idx=a.find(b);//在a中查找b.
        if(idx == string::npos )//不存在。
            cout << "not found ";
        else//存在。
            cout <<"found "
        idx=a.find(c);//在a中查找c。
        if(idx == string::npos )//不存在。
            cout << "not found ";
        else//存在。
            cout <<"found "
        return 0;
    }
  • 相关阅读:
    1041. 困于环中的机器人
    95. 不同的二叉搜索树 II
    LeetCode945:使数组唯一的最小增量
    LeetCode:925.长按键入
    LeetCode:926. 将字符串翻转到单调递增
    InteliJ 安装PlantUML插件
    CodeBlock换肤
    正则表达式验证手机号和座机号
    C#中使用反射遍历一个对象属性和值以及百分数
    c#中@的用法
  • 原文地址:https://www.cnblogs.com/ceerqingting/p/10559644.html
Copyright © 2011-2022 走看看