检查字符串是否包含另一串字符串(c++)
在c++中检查字符串是否包含另一串字符串,这个本来是我做过的一个算法题,不过最近刚好有个需求让我想到了这个题,就在此记录一下!
- 使用
std::string::findfunction
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)}">
随机推荐
- 《C++ Primer》Chapter 7 [类]
前言 在C++中,我们使用类定义自己得数据类型/通过定义新的类型来反应待解决的题的各种概念,是我们更容易编写.调试和修改程序. 我们需要主要关注数据抽象的重要性.数据抽象能帮助我们将对象的具体实现与对 ...
- HDU5589 Tree【分块 01字典树】
HDU5589 Tree 题意: 给出一棵\(N\)个点的树,每条边有边权,每次询问下标为\([L,R]\)区间内的点能选出多少点对,点对之间的路径上的边权异或和大于\(M\) 题解: 对于两点\(u ...
- 【uva 1349】Optimal Bus Route Design(图论--网络流 二分图的最小权完美匹配)
题意:有一个N个点的有向带权图,要求找若干个有向圈,使得每个点恰好属于一个圈.请输出满足以上条件的最小权和. 解法:有向圈?也就是每个点有唯一的后继.这是一个可逆命题,同样地,只要每个点都有唯一的后继 ...
- Codeforces Round #501 (Div. 3) B. Obtaining the String (思维,字符串)
题意:有两个字符串\(S\)和\(T\),判断\(T\)是否能由\(S\)通过交换某位置的相邻字符得到,如果满足,输出交换次数及每次交换的位置,否则输出\(-1\). 题解:首先判断不满足的情况,只有 ...
- Educational Codeforces Round 94 (Rated for Div. 2) C. Binary String Reconstruction (构造)
题意:给你一个字符串\(s\),原字符串为\(w\),如果\(i>x\)且\(w_{i-x}=1\),那么\(s_{i}=1\),如果\(i+x\le n\)且\(w_{i+x}=1\),那么\ ...
- 梨子带你刷burp练兵场(burp Academy) - 服务端篇 - Sql注入配套漏洞讲解笔记
目录 Sql注入 什么是Sql注入呢? Sql注入有哪些例子? 检索隐藏数据 打破应用逻辑 利用Union进行跨库查询 如何确定利用Union的注入攻击所需的列数呢? 如何确定Union的查询结果中哪 ...
- 【cpp上】课后正误小题
State whether each of the following is true or false. If false, explain why. Assume the state ment u ...
- windows hook + pyhook3 + python win32api hook + C 键盘hook
安装pyhook3见:https://www.cnblogs.com/lqerio/p/12096710.html 使用见:https://www.cnblogs.com/lqerio/p/12106 ...
- 局部变量 static new 结构体指针
struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; 有一个函数利用LisNode ...
- codeforces 6D
D. Lizards and Basements 2 time limit per test 2 seconds memory limit per test 64 megabytes input st ...