여기서 아이디어는 두 개의 문자열을 입력으로 받아 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