转-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:查某字符(串)是否包含在其他字符串中,返回字符串中指定表达式的起始 ...
随机推荐
- css > 的写法 html
.userInfo-view .info .name::after { content: " "; display: inline-block; height: 12rpx; wi ...
- wsl中加载git之后,发现文件是修改状态
查看git status,发现所有文件都被修改. git diff文件查看,发现是行尾的问题导致的. https://github.com/Microsoft/WSL/issues/184 在wsl里 ...
- Linux(Ubuntu)常用命令(二)
归档管理: 打包: tar -cvf xxx.tar 打包对象 (一般来说就是 -cvf 一起用)但这种不压缩的打包通常不用,接下来会说. -options:-c 生成档案文件,创建打包文件. ...
- go 语言结构控制
if else 结构: #第一种 if condition { // do something } #第二种 if condition { // do something } else { // d ...
- How To Release and/or Renew IP Addresses on Windows XP | 2000 | NT
Type 'ipconfig' (without the quotes) to view the status of the computer's IP address(es). If the com ...
- windows修改docker的默认存放位置
docker默认存储到c盘,我需要移动到其他盘. 参考了网上很多资料,结果要么移动不了,要么重启docker就回到c盘了. 最后参考docker的官方论坛,找到了解决方案.https://forums ...
- 下载Spring各个版本的jar包
网址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframew ...
- 2018-2-13-win10-uwp-绑定密码
title author date CreateTime categories win10 uwp 绑定密码 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...
- go语言从例子开始之Example23.通道缓冲
默认通道是 无缓冲 的,这意味着只有在对应的接收(<- chan)通道准备好接收时,才允许进行发送(chan <-).可缓存通道允许在没有对应接收方的情况下,缓存限定数量的值. 不支持缓冲 ...
- 关于ovf导入到Esxi上出显的“文件条目(行1)无效,sha256……”处理办法
通常删除同名文件*.mf文件即可导入!