简体   繁体   English

在JavaScript中查找常见字符串

[英]Finding common strings in javascript

I am trying to find common strings in given two strings. 我试图在给定的两个字符串中找到通用的字符串。

Example: 例:

string1 = "mega,cloud,two,website,final"
string2 = "window,penguin,literature,network,fun,cloud,final,sausage"
answer = "cloud,final,two"

So far this is what i got: 到目前为止,这就是我得到的:

function commonWords(first, second) {
    var words = first.match(/\w+/g);
    var result = "";
    words.sort();
    for(var i = 0; i < words.length; i++){
        if(second.includes(words[i])){
            result = result.concat(words[i]);
            result += ",";
        }
    }
    result = result.substr(0, result.length -1);
    return result;

}

But the result that i got is : 但是我得到的结果是:

answer = cloud,final

Can you help me out please? 你能帮我吗? First time asking question on StackOverFlow, so sorry for the typing. 第一次在StackOverFlow上提问,非常抱歉。

What I would do is first split on the two strings, then do a reduce and check if the other string includes the current string. 我要做的是先在两个字符串上拆分 ,然后进行reduce ,然后检查另一个字符串是否包含当前字符串。 If so add them to a new array. 如果是这样,请将其添加到新阵列。

 const string1 = "mega,cloud,two,website,final" const string2 = "window,penguin,literature,network,fun,cloud,final,sausage" const array1 = string1.split(',') const array2 = string2.split(',') const result = array1.reduce((arr, val) => array2.includes(val) ? arr.concat(val) : arr, []) console.log(result) // Convert it to a string if desired console.log(result.toString()) 

I would convert each string to an array of words with .split(',') . 我会使用.split(',')将每个字符串转换为单词数组。 Then filter the first array with .filter by checking if each item of the array is in the other one: 然后通过检查数组的每个项是否在另一个项中,使用.filter过滤第一个数组:

 string1 = "mega,Cloud,two,website,final" string2 = "window,penguin,literature,network,fun,cloud,final,sausage" array1 = string1.toLowerCase().split(',') array2 = string2.toLowerCase().split(',') var a = array1.filter(word => -1 !== array2.indexOf(word)); console.log(a) 

Before converting the string to an array I have applied .toLowerCase() so that the script can return cloud in the result if it appears in the first one as " C loud" and in the second as " c loud" 在将字符串转换为数组之前,我已经应用了.toLowerCase()以便脚本可以在结果中返回cloud ,如果它在第一个中显示为“ C Loud”,在第二个中显示为“ c Loud”

Try the following: 请尝试以下操作:

 string1 = "mega,cloud,two,website,final" string2 = "window,penguin,literature,network,fun,cloud,final,sausage"; var arr1= string1.split(","); var arr2 = string2.split(","); var result = []; arr1.forEach(function(str){ arr2.forEach(function(str2){ if(str2.indexOf(str) != -1) result.push(str); }); }); var answer = result.join(","); console.log(answer); 

The problem with your function is you also converting the string to into second as an array. 函数的问题是您还将字符串转换为第二个数组。

function commonWords(first, second) {
    var words = first.match(/\w+/g); // word is a array type
    var result = ""; 
    words.sort();
    for(var i = 0; i < words.length; i++){
        if(second.includes(words[i])){ //second should be string type
            result = result.concat(words[i]);
            result += ",";
        }
    }
    result = result.substr(0, result.length -1);
    return result;

}

Or you can try this 或者你可以尝试一下

    first = "mega,cloud,two,website,final"
second = "window,penguin,literature,network,fun,cloud,final,sausage"

function commonWords(first, second) {
    var words = first.match(/\w+/g);
    var result = "";
    words.sort();
    for(var i = 0; i < words.length; i++){
        if(second.includes(words[i])){
            result = result.concat(words[i]);
            result += ",";
        }
    }
    result = result.substr(0, result.length -1);
    return result;

}

console.log(commonWords(first, second));

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

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