Let say I've String
var str = 'Sumur Bandung'
and
var x = 'Kecamatan Sumur Bandung'
from str
and x
there are two matching characters Sumur
and Bandung
. How can I check that str
has characters that match with x
?
let str = "Sumur Bandung";
let x = "Kecamatan Sumur Bandung";
function stringContains(parentString, childString) {
const parentStringSeparator = parentString.split(" ");
return childString
.split(" ")
.every((word) => parentStringSeparator.includes(word));
}
console.log(stringContains(x, str));
If I understand you correctly, this is what you're asking. Given a parent string separated by spaces, check if every word of a child string is in the parent string.
Edit: This function doesn't take in account word order and splits every string with spaces.
Edit2: If you're trying to ask whether a child string contains at least one word from a parent string, you should use some instead of every:
let str = "Sumur Bandung";
let x = "Kecamatan Sumur Bandung";
function stringContains(parentString, childString) {
const parentStringSeparator = parentString.split(" ");
return childString
.split(" ")
.some((word) => parentStringSeparator.includes(word));
}
console.log(stringContains(x, str));
You can use " include
", it's the best.
var x = 'Kecamatan Sumur Bandung' var str = 'Sumur Bandung' console.log(x.includes(str) || str.includes(x))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.