检查字符串是否包含另一串字符串(c++)
在c++中检查字符串是否包含另一串字符串,这个本来是我做过的一个算法题,不过最近刚好有个需求让我想到了这个题,就在此记录一下!
- 使用
std::string::find
function
string str ("There are two needles in this haystack.");
string str2 ("needle");
if (str.find(str2) != string::npos) {
//.. found.
//如果不等,则说明找到一样的,如果相等,则说明没找到,可以查看find的具体用法:http://www.cplusplus.com/reference/string/string/find/
}
- 自己编写程序
#include <iostream>
#include <string>
bool CheckSubstring(std::string firstString, std::string secondString)
{
if (secondString.size() > firstString.size())
return false;
for (int i = 0; i < firstString.size(); i++)
{
int j = 0;
// If the first characters match
if (firstString[i] == secondString[j])
{
int k = i;
while (firstString[i] == secondString[j] && j < secondString.size())
{
j++;
i++;
}
if (j == secondString.size())
return true;
else // Re-initialize i to its original value
i = k;
}
}
return false;
}
int main()
{
std::string firstString, secondString;
std::cout << "Enter first string:";
std::getline(std::cin, firstString);
std::cout << "Enter second string:";
std::getline(std::cin, secondString);
if (CheckSubstring(firstString, secondString))
std::cout << "Second string is a substring of the frist string.\n";
else
std::cout << "Second string is not a substring of the first string.\n";
return 0;
}
- 使用
boost
库,在boost
中,你可以只使用boost::algorithm::contains:
#include "string"
#include "boost/algorithm/string.hpp"
using namespace std;
using namespace boost;
int main(){
string s("Hello Word!");
string t("ello");
bool b = contains(s, t);
cout << b << endl;
return 0;
}
如果您有更好的算法或者方法请私信或者评论我!
检查字符串是否包含另一串字符串(c++)的更多相关文章
- JavaScript确定一个字符串是否包含在另一个字符串中的四种方法
一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...
- 【转载】C#通过IndexOf方法判断某个字符串是否包含在另一个字符串中
C#开发过程中针对字符串String类型的操作是常见操作,有时候需要判断某个字符串是否包含在另一个字符串,此时可以使用IndexOf方法以及Contain方法来实现此功能,Contain方法返回Tru ...
- 判断一字串String中是否包含某一串字符串
String ostype = data.getString("osType").toUpperCase(); //转换为大写 if (ostype.contains(" ...
- C# 中请使用Contains判断字符串是否包含另一段字符串
∵ :使用Contains 比 IndexOf 的性能要高很多. 因为 Contains 是判断某个字符串是否在该字符串里面,而IndexOf是返回对应下标值 但是在使用contains的时候,注意转 ...
- 写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。如字符串”PEN”包含在字符串“INDEPENDENT”中。
package lovo.test; import java.util.Scanner; public class Java { @param args public static void main ...
- C# 中判断字符串是否包含另一段字符串,请使用 Contains
使用:Contains 比 IndexOf 的性能提高很多. 因为 Contains 是判断某个字符串是否在另外一个字符串中,而IndexOf需要返回下标值.
- Python判断一个字符串是否包含某个指定的字符串
成员操作符 in str = "string test string test" find1 = "str" find2 = "test" ...
- java中怎么判断一个字符串中包含某个字符或字符串
public static void main(String[] args) { String str="ABC_001"; ){ System.out.println(" ...
- 用jstl标签判断一个字符串是否包含了另一个字符串
<c:if test="${fn:contains(str1,str2)}">
随机推荐
- Codeforces Round #691 (Div. 2) C. Row GCD (数学)
题意:给你两个数组\(a\)和\(b\),对于\(j=1,...,m\),找出\(a_1+b_j,...,a_n+b_j\)的\(gcd\). 题解:我们很容易的得出\(gcd\)的一个性质:\(gc ...
- AtCoder Beginner Contest 173 C - H and V (二进制枚举)
题意:有一张图,.表示白色,#表示黑色,每次可以将多行和多列涂成红色(也可不涂),问有多少种方案,使得剩下黑点的个数为\(k\). 题解:又学到了新的算法qwq,因为这题的数据范围很小,所以可以用二进 ...
- EFCore学习记录--数据访问技术人门2
1 code fist 1.创建实体类: 2.创建DbContext类: mysql连接字符串是:Server=127.0.0.1;Port=3306;Database=BlogDb; User=ro ...
- 第三方库:logger,自定义日志封装模块
为了使用方便,二次封装logger. import os import datetime from loguru import logger class Logings: __instance = N ...
- 大规模数据爬取 -- Python
Python书写爬虫,目的是爬取所有的个人商家商品信息及详情,并进行数据归类分析 整个工作流程图: 第一步:采用自动化的方式从前台页面获取所有的频道 from bs4 import Beautiful ...
- Linux系统启动过程内核文件丢失解决方法
一.问题描述 公司近期因机房断电,导致服务器重启后,引导进入不了操作系统.经过检查发现启动文件缺失,导致系统启动失败,网上搜了好多资料,解决都比较零散,现结合实际处理经验和网友的建议整理接方案. 二. ...
- 数位dp【模板 + 老年康复】
学习博客: 戳这里 戳这里 "在信息学竞赛中,有这样一类问题:求给定区间中,满足给定条件的某个D 进制数或此类数的数量.所求的限定条件往往与数位有关,例如数位之和.指定数码个数.数的大小顺序 ...
- 阿里巴巴java开发手册(2020版)
阿里巴巴java开发手册(2020版) 2020版 链接: pan.baidu.com/s/1Zls_FUBK- 密码: titz 2019版 链接: pan.baidu.com/s/1cvCVQvj ...
- 如何使用 js 检测控制台被用户打开了
如何使用 js 检测控制台被用户打开了 js solutions 监听 F12 事件 监听键盘快捷键组合 Ctrl + Shift + I Option + Command + I Object.to ...
- cookie & session & token compare
cookie & session & token compare cookie.session.token 区别和优缺点 存储位置 cookie 存在 client 端 session ...