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 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
?
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))
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));