Java implements string matching by itself

Source: Internet
Author: User

 Packagecom.learn.algorithm.Str;/*** Self-fulfilling string matching *@authorJiekun.cui **/ Public classsstring { Public Static voidMain (string[] args) {System.out.println (IndexOf ("China", "in", 0)); }            /*** String Matching algorithm--find the location of S2 in S1 *@paramS1 *@paramS2 *@paramlocation where POS starts to match *@return     */     Public Static intIndexOf (String s1,string S2,intPOS) {                intL1 =s1.length (); intL2 =s2.length (); if(l2==0) {            return-1; }                intI1=pos,i2=0;  while(I1 < L1 && I2 <L2) {            if(S1.charat (i1) = =S2.charat (I2)) {I1++ ; I2++ ; } Else{I1= I1-i2 + 1;//go back to the next position where the match starts and no match succeeds, starting the next match;i2 = 0; }        }                if(I2 <=L2) {            returnI1-I2; }                return-1; }}

Java implements string matching by itself

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.