在c++中检查字符串是否包含另一串字符串,这个本来是我做过的一个算法题,不过最近刚好有个需求让我想到了这个题,就在此记录一下!

  1. 使用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/
}
  1. 自己编写程序
#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;
}
  1. 使用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++)的更多相关文章

  1. JavaScript确定一个字符串是否包含在另一个字符串中的四种方法

    一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...

  2. 【转载】C#通过IndexOf方法判断某个字符串是否包含在另一个字符串中

    C#开发过程中针对字符串String类型的操作是常见操作,有时候需要判断某个字符串是否包含在另一个字符串,此时可以使用IndexOf方法以及Contain方法来实现此功能,Contain方法返回Tru ...

  3. 判断一字串String中是否包含某一串字符串

    String ostype = data.getString("osType").toUpperCase(); //转换为大写 if (ostype.contains(" ...

  4. C# 中请使用Contains判断字符串是否包含另一段字符串

    ∵ :使用Contains 比 IndexOf 的性能要高很多. 因为 Contains 是判断某个字符串是否在该字符串里面,而IndexOf是返回对应下标值 但是在使用contains的时候,注意转 ...

  5. 写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。如字符串”PEN”包含在字符串“INDEPENDENT”中。

    package lovo.test; import java.util.Scanner; public class Java { @param args public static void main ...

  6. C# 中判断字符串是否包含另一段字符串,请使用 Contains

    使用:Contains 比 IndexOf 的性能提高很多. 因为 Contains 是判断某个字符串是否在另外一个字符串中,而IndexOf需要返回下标值.

  7. Python判断一个字符串是否包含某个指定的字符串

    成员操作符 in str = "string test string test" find1 = "str" find2 = "test" ...

  8. java中怎么判断一个字符串中包含某个字符或字符串

    public static void main(String[] args) { String str="ABC_001"; ){ System.out.println(" ...

  9. 用jstl标签判断一个字符串是否包含了另一个字符串

    <c:if test="${fn:contains(str1,str2)}">

随机推荐

  1. Codeforces Round #672 (Div. 2)

    比赛链接:https://codeforces.com/contest/1420 A. Cubes Sorting 题意 给出一个大小为 $n$ 的数组 $a$,每次只可以交换相邻的两个元素,最多交换 ...

  2. 【uva 11093】Just Finish it up(算法效率--贪心)

    题意:环形跑道上有N个加油站,编号为1~N.第 i 个加油站可以加油Ai加仑,从加油站 i 开到下一站需要Bi加仑汽油.问可作为起点走完一圈后回到起点的最小加油站编号. 解法:我们把每个加油站的Ai, ...

  3. hdu 1166 敌兵布阵 线段树区间修改、查询、单点修改 板子题

    题目链接:敌兵布阵 题目: C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视 ...

  4. Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)

    C. Link Cut Centroids Fishing Prince loves trees, and he especially loves trees with only one centro ...

  5. 信号量解决写者优先&读者优先&公平竞争(reader writer)

    先说问题: 这里的rand都是伪随机.解决也很简单,srand即可.内容懒得改了~~ 描述及思路:           代码:           运行结果:   读者优先:           效果 ...

  6. Leetcode(28)-实现strStr()

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  7. 最新 React 源码学习笔记

    最新 React 源码学习笔记 v17.x.x 框架架构 核心算法 设计模式 编码风格 项目结构 为什么出现 解决了什么问题 有哪些应用场景 refs https://github.com/learn ...

  8. 使用 js 实现十大排序算法: 希尔排序

    使用 js 实现十大排序算法: 希尔排序 希尔排序 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  9. Kotlin 入门教程

    Kotlin 入门教程 Android / Java https://developer.android.com/kotlin?hl=zh-cn 使用 Kotlin 开发 Android 应用 使用 ...

  10. MDN 文档高级操作进阶教程

    MDN 文档高级操作进阶教程 MDN 文档, 如何优雅的使用 MDN 文档上的富文本编辑器 pre & 语法高亮器 code & note box source code 上传附件 i ...