public class patternmatching { /* * This method is used to determine if a substring exists in the parent string If there is a return true, Otherwise, False searchme represents the parent string, and substring represents the substring */ public boolean match (string Searchme, string substring) { boolean flag = true; // flag represents a tag variable, Used to determine if the match succeeds int max = searchme.length () - substring.length ();// max is offset for (int i = 0; i <= max; i++) { flag = true; for (Int j = 0; j < substring.length (); j++) { if (Searchme.charat (I + j) != substring.charat (j)) { flag = false;// Description Matching failed } } if (flag) {// description match success break; } } return flag; } /* This method is used to print the index where the substring first appears in the parent string */ public int Matchindex (STRING&NBSP;SEARCHME,&NBSP;STRING&NBSp;substring) { boolean flag = true; // flag represents a tag variable that is used to determine if the match is successful int index = 0;// index This variable is used to represent the first index position of a substring in the parent string int max = searchme.length ( ) - substring.length ();// max is offset for (int i = 0; i <= max; i++) { flag = true; for (int j = 0; j < substring.length (); j++) { if (Searchme.charat (i + j) != Substring.charat (j)) { flag = false;// description match failed } } if (flag) {// Description Match Successful index = i; break; } } return index; } /* This method is used to find all locations where the substring appears in the parent string */ public stringbuffer matchallindex (String searchme, string substring) { boolean flag = true; // flag represents a tag variable, Used to determine if the match was successful int max = Searchme.length () - substring.length ();// max is offset stringbuffer index = new stringbuffer ("All locations where the substring appears in the parent string is:"); for (int i = 0; i <= max; i++) { flag = true; for (int j = 0; j < Substring.length (); j++) { if (Searchme.charat (i + j) != Substring.charat (j)) { flag = false;// description match failed } } if (flag) {// Description Match Success index.append (i + " ");// Use StringBuffer-specific method append to stitch all index positions of the substring in the first letter of the parent string continue; } } return index; } /* This method is used to replace substrings in the parent string with the specified string */ public string replace (string searchme, string substring,string replacation) { boolean flag = true; // flag represents a tag variable, Used to determine if the match succeeded int max = searchme.length () - substrinG.length ();// max is offset for (int i = 0; i <= max; i++) { flag = true; for (int j = 0; j < Substring.length (); j++) { if (Searchme.charat (i + j) != Substring.charat (j)) { flag = false;// description match failed } } if (flag) {// Description Match Success searchme = searchme.replace (substring, replacation); continue; } } return searchme; }}
Here is the test class: Atternmatchingtest
public class Patternmatchingtest {public static void main (string[] args) {String fatherstr = ' Look for a ' substring in me substring ";//Parent String sonstr =" subs ";//substring string replacation =" AAAA "; /* This print is used to determine if the match is successful */SYSTEM.OUT.PRINTLN (new Patternmatching (). Match (Fatherstr, sonstr)); /* This print is used to print out the index where the substring first appears in the parent string */System.out.println (new Patternmatching (). Matchindex (Fatherstr, sonstr)); /* This print is used to print all locations where the substring appears in the parent string */System.out.println (new Patternmatching (). Matchallindex (Fatherstr, sonstr)); /* This print is used to replace substrings in the parent string with the specified string */System.out.println (new Patternmatching (). replace (Fatherstr, SONSTR, replacation)); }}