简体   繁体   English

测试字符串是否包含构成另一个字符串的所有字符

[英]Test If String Contains All Characters That Make Up Another String

I am trying to use Javascript to see if a certain string contains all characters that make up another string.我正在尝试使用 Javascript 来查看某个字符串是否包含构成另一个字符串的所有字符。

For instance, the word "hello" contains all characters that make up the word "hell."例如,单词“hello”包含构成单词“hell”的所有字符。 Also, the word "hellowy" contains all characters that make up the word "yellow."此外,单词“hellowy”包含构成单词“yellow”的所有字符。

Most importantly, the method needs to work irrespective of the order of characters in both string.最重要的是,无论两个字符串中的字符顺序如何,该方法都需要工作。 In addition, the numbers of characters matters.此外,字符数也很重要。 "Hel" does not contain all characters to make up "hell." “Hel”不包含构成“hell”的所有字符。 This refers strictly to the number of characters: one needs two l's to make word "hell" and "hel" only has one.这严格指的是字符数:一个需要两个 l 来组成单词“hell”,而“hel”只有一个。

Further clarifying the question, I am not worried if I am left with some "unused" characters after the composition of the substring from the characters of the string.进一步澄清这个问题,我不担心在从字符串的字符组成子字符串之后是否留下一些“未使用”的字符。 That is, "helll" still should contain all letters for the word "hell."也就是说,“hell”仍然应该包含“hell”这个词的所有字母。

How can I accomplish this efficiently?我怎样才能有效地做到这一点? Perhaps there is a regex solution?也许有一个正则表达式解决方案? Speed is somewhat of an issue, but not absolutely critical.速度有点问题,但不是绝对关键。

You can use every :您可以使用every

function test(string, substring) {
    var letters = [...string];
    return [...substring].every(x => {
        var index = letters.indexOf(x);
        if (~index) {
            letters.splice(index, 1);
            return true;
        }
    });
}

Every will fail in the first falsy value, then it does not search every letter. Every将在第一个虚假值中失败,然后它不会搜索每个字母。

Edit, Updated编辑,更新

In addition, the numbers of characters matters.此外,字符数也很重要。 "Hel" does not contain all characters to make up "hell." “Hel”不包含构成“hell”的所有字符。

You can use a variable to store Boolean value, for..of loop, String.prototype.indexOf() check for, set Boolean variable, break loop if false .您可以使用变量来存储Boolean值, for..of循环, String.prototype.indexOf()检查,设置Boolean变量,如果为falsebreak循环。

You should also be able include check if input string .length is equal to matching string .length at if condition, set variable to false if the two string .length properties are not equal.您还应该能够在if条件下检查输入字符串.length是否等于匹配字符串.length ,如果两个字符串.length属性不相等,则将变量设置为false

var str = "hell";
var match = "hel";
var bool = true; 
for (var prop of str) {
  if (str.length !== match.length || match.indexOf(prop) === -1) {
    bool = false; break;
  }
};
console.log(bool); // false

If the number of letters matters then maybe something like this:如果字母的数量很重要,那么可能是这样的:

function test(string, substring) {
    var regexp = new RegExp(substring.split("").sort().map(function(s) { return s + "+"; }).join(""));
    return regexp.test(string.split("").sort().join(""));
}

This is slower than the above answers, but if there is some repetition in the strings then it's possible to cache and get better speed performance than the other answers:这比上述答案慢,但如果字符串中有一些重复,则可以缓存并获得比其他答案更好的速度性能:

var cache1 = { };
var cache2 = { };
function test2(string, substring) {
    var regexp = cache1[substring];
    if (!regexp) {
        regexp = new RegExp(substring.split("").sort().map(function(s) { return s + "+"; }).join(""));
        cache1[substring] = regexp;
    }

    var string2 = cache2[string];
    if (!string2) {
        string2 = string.split("").sort().join("");
        cache2[string] = string2;
    }

    return regexp.test(string2);
}

Make some training I came up with this thing:做一些培训我想出了这个东西:

function test(str, substring) {
  var arr_str = str.toLowerCase().split('');
  var arr_substr = substring.toLowerCase().split('');

  return arr_substr.filter(function(each) {
    return arr_str.indexOf(each) === -1;
  }).length === 0;
}

console.log(test("Alien", "line")); // true
console.log(test("Hello", "hello")); // true
console.log(test("hello", "hey")); // false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM