在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. Who Gets the Most Candies?

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 11303   Accepted: 3520 Case Time Limit ...

  2. Codeforces Round #669 (Div. 2) A. Ahahahahahahahaha (构造)

    题意:有一个长度为偶数只含\(0\)和\(1\)的序列,你可以移除最多\(\frac{n}{2}\)个位置的元素,使得操作后奇数位置的元素和等于偶数位置的元素和,求新序列. 题解:统计\(0\)和\( ...

  3. MySQL 基础面试题

    请写出什么是事务? 事务是一组不可分割的 DML 语句,事务处理可以用来维护数据库的完整性,保证一组 SQL 语句要么全部执行成功,要么全部不执行,只有 InnoDB 存储引擎才支持事务 . 事务的特 ...

  4. 在Python中使用BeautifulSoup进行网页爬取

    目录 什么是网页抓取? 为什么我们要从互联网上抓取数据? 网站采集合法吗? HTTP请求/响应模型 创建网络爬虫 步骤1:浏览并检查网站/网页 步骤2:创建用户代理 步骤3:导入请求库 检查状态码 步 ...

  5. element ui 渲染超过上百条数据时页面卡顿,更流畅的加载大量数据

    问题:element ui table渲染上百条数据,页面渲染开始出现延时 解决方案:使用pl-table 注意:设置use-virtual并给定table高度

  6. go modules——HelloWorld示例

    go modules--HelloWorld示例 go modules 存在的意义是方便代码的共享(虽然这会使自己开发过程中有一些小小的麻烦) 开发第一步,创建一个github仓库,然后克隆到本地 首 ...

  7. Linux bash shell All In One

    Linux bash shell All In One Linux https://tinylab.gitbooks.io/shellbook/content/zh/chapters/01-chapt ...

  8. MongoDB 101

    MongoDB 101 Studio 3T https://studio3t.com/academy/ https://studio3t.com/academy/lessons/introducing ...

  9. CSS BEM

    CSS BEM Block, Element, Modifier https://en.bem.info/methodology/quick-start/ BEM /* Block component ...

  10. Chrome V8 系统架构

    Chrome V8 系统架构 Chromium 多进程多线程架构 design-documents https://www.chromium.org/developers/design-documen ...