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\n";//输出结果。
    else//否则存在。
        cout <<"found\n"//输出结果。
    if(strstr(a.c_str(), c) == NULL)//在a中查找b,如果不存在,
        cout << "not found\n";//输出结果。
    else//否则存在。
        cout <<"found\n"//输出结果。
    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\n";
    else//存在。
        cout <<"found\n"
    idx=a.find(c);//在a中查找c。
    if(idx == string::npos )//不存在。
        cout << "not found\n";
    else//存在。
        cout <<"found\n"
    return 0;
}

C/C++判断字符串是否包含某个子字符串的更多相关文章

  1. python 字符串是否包含某个子字符串

    方法如下:以后再整理 if str1 in str2: 包含的话,True if str1.find(str2)>=0: 包含的话,返回第一次出现的位置,没有的话为负数 https://www. ...

  2. Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串

    Jquery 选择器 详解   在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...

  3. String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。

    // 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...

  4. C#判断字符串中是否包含一个子字符串是可以直接使用Contains()方法

    1. 以前判断一个字符串中是否包含另一个子字符串时,习惯使用 IndexOf(); string str = "ABC@QQ"; if(str.IndexOf("@&qu ...

  5. js 判断字符串是否包含另外一个字符串

    示例代码: <script type="text/javascript"> var str = "测试一个字符串(ehtrzes)是否包含另外一个字符串&qu ...

  6. JS中判断某个字符串是否包含另一个字符串的五种方法

    String对象的方法 方法一: indexOf()   (推荐) ? 1 2 var str = "123" console.log(str.indexOf("2&qu ...

  7. java中如何判断一个字符串是否包含另外一个字符串的方法

    indexOf(String s)的使用,如果包含,返回的值是包含该子字符串在父类字符串中起始位置: 如果不包含必定全部返回值为-1 package my_automation; public cla ...

  8. 关于前端JS判断字符串是否包含另外一个字符串的方法总结

    RegExp 对象方法 test() var str = "abcd"; var reg = RegExp(/d/); console.log(reg.test(str)); // ...

  9. js判断某个字符串是否包含另一个字符串

    1.indexOf():推荐,可返回某个指定的字符串值在字符串中首次出现的位置.如果要检索的字符串值没有出现,则该方法返回 -1. var str = "123" console. ...

随机推荐

  1. 【WPS/Visio】WPS word无法复制或编辑Visio对象

    前言 Win10,WPS2019,Visio2016. 好像是有一次设置 .vsdx 的默认打开方式为Visio,之后每次在WPS里复制Visio对象,或双击编辑WPS word中以前的Visio对象 ...

  2. Django @csrf_exempt不能在类视图中工作(Django @csrf_exempt not working in class View)

    我在Django 1.9中有一个使用SessionMiddleware的应用程序.我想在同一个项目中为这个应用程序创建一个API,但是在做一个POST请求时,它不能使用@csrf_exempt注释. ...

  3. 毛玻璃效果 css

    毛玻璃效果 <style> .container{ width: 287px; height: 285px; background-image: url(img/background.pn ...

  4. java中使用SimpleDateFormat实现字符串和日期的相互转换

    java中使用SimpleDateFormat实现字符串和日期的相互转换 import java.text.ParseException; import java.text.SimpleDateFor ...

  5. 继续死磕python

    一.数据运算 算术运算 比较运算 赋值运算 逻辑运算 成员运算 身份运算 位运算 其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是 ...

  6. AtCoder Regular Contest 092 Two Sequences AtCoder - 3943 (二进制+二分)

    Problem Statement You are given two integer sequences, each of length N: a1,…,aN and b1,…,bN. There ...

  7. hdu 1087 最大递增子序列和

    #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...

  8. nginx_mirror_module流量复制在项目中的应用

    参考文档:https://my.oschina.net/andChow/blog/2873870 https://blog.csdn.net/lancerh/article/details/88645 ...

  9. MongoDB之自动启动服务

    安装详细步骤请点我 为了能让NoSQLBooster for MongoDB连接的时候不报错,将mongodb添加到系统服务中. 在C:\Program Files\MongoDB\Server\3. ...

  10. hihocode 1584 : Bounce (找规律)(2017 北京网络赛G)

    题目链接 比赛时随便找了个规律,然后队友过了.不过那个规律具体细节还挺烦的.刚刚偶然看到Q巨在群里提到的他的一个思路,妙啊,很好理解,而且公式写起来也容易.OrzQ巨 #include<bits ...