给定一串字符串,一个人怎么能确定在特定段落中是否所有字符串都存在?

时间:2018-07-28 21:53:21

标签: string full-text-search

例如,如果我有一个列表

list = ['brown', 'lazy', 'dog']

和两个句子:

1) "The quick brown fox jumps over the lazy dog."
2) "The quick brown fox jumps over the dog."

有没有办法确定所有字符串都出现在句子1中,而不是出现在句子2中?

1 个答案:

答案 0 :(得分:0)

一些想法:

重击:

输入来自命令行作为参数,如果不是数组中的所有单词都存在,脚本将以代码1退出并显示“未找到”消息。

std::vector<std::vector<int>> v
{
    { 0,0,0 },
    { 0,0,0 },
    { 0,0,0 }
};

PHP:

相同的概念,如果#!/bin/bash sentence=$1 declare -a arr=("brown" "lazy" "dog") for i in "${arr[@]}"; do if [[ $sentence != *"$i"* ]]; then echo "Not found" exit 1 fi done 字符串中未包含所有数组内容,则打印“未找到”。

$sentence

Python:

$words = array("brown", "lazy", "dog");
$sentence = "The quick brown fox jumps over the lazy dog.";
foreach($words as $word){
    if(strpos($sentence, $word) === false){
        echo "Not found";
    }
}

Ruby:

words = ["brown", "lazy", "dog"]
sentence = "The quick brown fox jumps over the lazy dog."
for word in words:
    if word not in sentence:
        print "Not found"