Home > Web Front-end > JS Tutorial > body text

js function implementation method to determine the largest substring of two strings

高洛峰
Release: 2016-12-08 13:08:03
Original
1212 people have browsed it

As shown below:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<script type="text/javascript">
function search(str1,str2)
{
var i=j=k=a=jk=kk=0;
var m=str1.length;
var n=str2.length;
var index=0;
var maxlen=0;
var str="";
var flag=0;
for (i=0;i<n;i++)
{
k=i;
for (j=0;j<m;j++)
{
a = 0;
flag = 0;
jk=j;
kk=k;
while(jk<=m&&str1[kk]==str2[jk])
{
kk++;jk++;a++;
flag = 1;
}
if(flag==1)
{
if(a>maxlen)
{
maxlen = a;
index = jk-a;//匹配的首项
}
}
}
 
}
for(;maxlen>0;maxlen--)
{
str+=str2[index++];
 
}
return str;
}
alert(search("kssd","ssdfa"));
</script>
<body>
<div>11</div>
</body>
</html>
Copy after login


Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!