转-C++之string判断字符串是否包含某个子串
转自:https://blog.csdn.net/zhouxinxin0202/article/details/77862615/
1、string类函数find
C++的string类提供了字符串中查找另一个字符串的函数find。
其重载形式为:
string::size_type string::find(string &);
功能为在string对象中,查找参数string类型的字符串是否存在,如果存在,返回起始位置。不存在则返回 string::npos。
e.g.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a="abcdefghigklmn";
string b="def";
string c="";
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;
}
2、strstr函数
在C语言中,字符串存储为字符数组,以'\0'结束。 在C的接口中,有strstr函数,可以在字符串中查找另一个字符串。
char * strstr(const char *str1, const char *str2);
功能为在str1中查找str2,如果存在,那么返回查找到的起始指针,否则返回NULL。
e.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string a="abcdefghigklmn";
char *b="def";
char *c=""; 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中查找c,如果不存在,
cout << "not found\n";//输出结果。
else//否则存在。
cout <<"found\n"; //输出结果。
return ;
}
转-C++之string判断字符串是否包含某个子串的更多相关文章
- 判断字符串是否包含字母‘k’或者‘K’
判断字符串是否包含字母‘k’或者‘K’ public bool IsIncludeK(string temp) { temp = temp.ToLower(); if (temp.Contains(' ...
- 【功能代码】---3 JS判断字符串是否包含某个字符串
JS判断字符串是否包含某个字符串 var str ="abc"; if(str.indexOf("bc")>-1){ alert('str中包含bc字符串 ...
- Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串
Jquery 选择器 详解 在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...
- JS判断一个字符串是否包含一个子串函数.
微信小程序 JS判断一个字符串是否包含一个子串函数. //str 字符串,name子串 contains:function(str,name){ if(str.indexOf( ...
- Java:判断字符串中包含某字符的个数
Java:判断字符串中包含某字符的个数 JAVA中查询一个词在内容中出现的次数: public int getCount(String str,String key){ if(str == null ...
- Js判断一个字符串是否包含一个子串
Js中经常遇到判断一个字符串是否包含一个子串,java语言中有containes的方法,直接调用就可以了.除非引用第三方数据库,Js中没有contains方法. 为了实现更java语言中contain ...
- C/C++判断字符串是否包含某个字符串
C风格 #include <iostream> #include <string> #include <cstring> using namespace std; ...
- js判断字符串是否包含指定的字符
判断字符串是否包含指定字符是很常用的功能,比如说,注册时用户名限制不能输入"管理员",或者需要js判断url跳转链接是否包含某个关键词等-- <!DOCTYPE html&g ...
- SQL中判断字符串中包含字符的方法
通过2个函数CHARINDEX和PATINDEX以及通配符的灵活使用 函数:CHARINDEX和PATINDEX CHARINDEX:查某字符(串)是否包含在其他字符串中,返回字符串中指定表达式的起始 ...
随机推荐
- 2018-2019-2 网络对抗技术 20165206 Exp7 网络欺诈防范
- 2018-2019-2 网络对抗技术 20165206 Exp7 网络欺诈防范 - 实验任务 (1)简单应用SET工具建立冒名网站 (1分) (2)ettercap DNS spoof (1分) ...
- JSON的android应用实例
JSON的android应用实例 Json在线解析器 下面是直接通过JUnit来测试直接通过API来解析Json数据 1.普通键值对象 2.Json数组对象 3.Json数组对象
- CentOS 如何将.deb 文件 转换.rpm, centos安装deb包
CentOS 如何将.deb 文件 转换.rpm [root@localhost tmp]#tar zxvf alien_8.88.tar.gz yum install alien [root@loc ...
- English-Words with 'ir'
hire thirty thirteen third sir birthday shirt stir circle dirty skirt affirm affirmation affirmable ...
- 嵌入式C语言3.4 关键字---类型描述符auto/register/static/const/extern/volatile/
对内存资源存放位置的限定 1. auto 默认值---分配的内存都是可读可写的区域 auto int a; 区域如果出现 {} 我们认为在栈空间 2. register register int a; ...
- SpringMVC表单验证器
本章讲解SpringMVC中怎么通过注解对表单参数进行验证. SpringBoot配置 使用springboot,spring-boot-starter-web会自动引入hiberante-valid ...
- Flink从socket读取数据sink到redis
package com.lin.flink.stream.customPartition; import org.apache.flink.api.common.functions.MapFuncti ...
- notepad++编辑软件
官网:http://notepad-plus-plus.org/ 官网截图(2015/4/29): 版本: Notepad++ 6.7.7 下载地址:http://notepad-plus-plus. ...
- C#编程--第二天
一.变量:变量先声明,后赋值,再使用. 语法:变量类型 变量名=值: 变量类型: 分为基本数据类型和引用类 基本数据类型:整型.浮点型.字符型.布尔型 引用类:字符串.日期时间.枚举类型.结构类型 i ...
- elasticsearch 关联单词查询以及Shingles
Shingle Token Filter A token filter of type shingle that constructs shingles (token n-grams) from a ...