Find the longest contiguous substring that appears in both strings using an iterative approach.
Code
Algorithmslet longest = '', current = '';
for (let i = 0; i < a.length; i++) {
for (let j = i + 1; j <= a.length; j++) {
current = a.slice(i, j);
if (b.includes(current) && current.length > longest.length) longest = current;
}
}
return longest;Parameters
First string
Second string
Browser·fetch() may be limited by CORS
More JavaScript Snippets
Capitalize First Letter
Capitalize the first letter of a string while keeping the rest unchanged.
Center String
Center a string within a given width by padding with spaces.
Check if Palindrome
Check if a string is a palindrome by comparing characters from both ends.
Compare Strings (Case Insensitive)
Compare two strings ignoring case differences.
Convert to Camel Case
Convert a string to camelCase format with lowercase first letter.
Convert to Kebab Case
Convert a string to kebab-case format.