我正在尝试在给定的两个字符串中找到常用字符串。
示例:
string1 = "mega,cloud,two,website,final"
string2 = "window,penguin,literature,network,fun,cloud,final,sausage"
answer = "cloud,final,two"
到目前为止,这是我得到的:
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;
}
但我得到的结果是:
answer = cloud,final
你可以帮帮我吗?第一次在StackOverFlow上提问,很抱歉打字。
答案 0 :(得分:1)
我要做的是在两个字符串上首先split,然后执行reduce并检查另一个字符串是否包含当前字符串。如果是这样,请将它们添加到新数组中。
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())
&#13;
答案 1 :(得分:0)
我会将每个字符串转换为.split(',')
的单词数组。然后通过检查数组的每个项是否在另一个项中来过滤第一个数组.filter
:
-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: a3e4a735ee8304dd50f5af53a64d7b639894cbcc24ba01d0171a588e67455018
name: gitchapter
version: 0.1.0.0
author: Chris Stryczynski
maintainer: Chris Stryczynski
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10
executable app
main-is: Main.hs
other-modules:
BlogLiterately
Example
FileSection
GHCi
GHCiSession
Git
GitTextPartial
Hart
Operations
Operations.Parsers
QuasiText
Render
Section
Test
Paths_gitchapter
hs-source-dirs:
src
build-depends:
HUnit
, QuickCheck
, base >=4.9 && <4.11
, directory
, extra
, filepath
, foldl
, mtl
, optparse-applicative
, pandoc-include-code
, parsec
, pretty-simple
, process
, regex-pcre
, regex-posix
, safe
, string-conversions
, system-filepath
, template-haskell
, text
, transformers
, turtle
, unix
, unordered-containers
default-language: Haskell2010
executable test
main-is: Test.hs
other-modules:
BlogLiterately
Example
FileSection
GHCi
GHCiSession
Git
GitTextPartial
Hart
Main
Operations
Operations.Parsers
QuasiText
Render
Section
Paths_gitchapter
hs-source-dirs:
src
build-depends:
HUnit
, QuickCheck
, base >=4.9 && <4.11
, directory
, extra
, filepath
, foldl
, mtl
, optparse-applicative
, pandoc-include-code
, parsec
, pretty-simple
, process
, regex-pcre
, regex-posix
, safe
, string-conversions
, system-filepath
, template-haskell
, text
, transformers
, turtle
, unix
, unordered-containers
default-language: Haskell2010
&#13;
在将字符串转换为数组之前,我已应用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)
,以便脚本可以在结果中返回.toLowerCase()
,如果它出现在第一个中,那么&#34; C 强>响&#34;在第二个中&#34; c 响亮&#34;
答案 2 :(得分:0)
尝试以下方法:
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);
答案 3 :(得分:0)
您的功能问题是您还将字符串转换为第二个数组。
NA
或者你可以试试这个
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;
}