JavaScript逐字比较两个句子,如果两个子句彼此为子串,则返回
这里的想法是将两个字符串作为输入,如果a是b的子字符串或b是a的子字符串,则返回true,否则返回false。
例如-
isSubstr(‘hello’, ‘hello world’) // true isSubstr(‘can I use’ , ‘I us’) //true isSubstr(‘can’, ‘no we are’) //false
因此,在该函数中,我们将检查较长的字符串,一个字符串包含更多字符,并检查另一个字符串是否是其子字符串。
这是这样做的代码-
示例
const str1 = 'This is a self-driving car.';
const str2 = '-driving c';
const str3 = '-dreving';
const isSubstr = (first, second) => {
if(first.length > second.length){
return first.includes(second);
}
return second.includes(first);
};
console.log(isSubstr(str1, str2));
console.log(isSubstr(str1, str3a));输出结果
控制台中的输出将为-
true false
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿