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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP