JavaScript Custom Function Implementation method to find the longest common substring of two strings _javascript tips

Source: Internet
Author: User

The example in this article describes the method that JavaScript custom function implements to find the longest common substring of two strings. Share to everyone for your reference, specific as follows:

Finds the longest common substring of two strings
function Findsubstr (s1,s2) {
  var s=sstr= "", L1=s1.length,l2=s2.length;
  if (L1>L2) {var s3=s1;s1=s2,s2=s3,l1=s2.length;}
  for (var j=l1;j> 0; j--) for
    (var i= 0; i<=l1-j;i++) {
      sstr = s1.substr (i,j);
      if (S2.indexof (SSTR) >= 0) return sstr;
    }
  Return "";
}
Document. Writeln (Findsubstr ("aaa3333", "baa333cc")); aa333
document. Writeln (Findsubstr ("aaax3333--", "baa333ccx3333333x"));//x3333

More readers interested in JavaScript-related content can view this site: "JavaScript in the JSON Operation tips Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"

I hope this article will help you with JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.