/** ********************* * Locate the position of a substring. * * @param paraString The given substring. * @return The first position. -1 for no matching. ********************* */ publicintlocate(MyString paraMyString) { booleantempMatch=false; for (inti=0; i < length - paraMyString.length + 1; i++) { // Initialize. tempMatch = true; for (intj=0; j < paraMyString.length; j++) { if (data[i + j] != paraMyString.data[j]) { tempMatch = false; break; } // Of if } // Of for j
if (tempMatch) { return i; } // Of if } // Of for i return -1; }// Of locate
/** ********************* * Get a substring * * @param paraString The given substring. * @param paraStartPosition The start position in the original string. * @param paraLength The length of the new string. * @return The first position. -1 for no matching. ********************* */ public MyString substring(int paraStartPosition, int paraLength) { if (paraStartPosition + paraLength > length) { System.out.println("The bound is exceeded."); returnnull; } // Of if
MyStringresultMyString=newMyString(); resultMyString.length = paraLength; for (inti=0; i < paraLength; i++) { resultMyString.data[i] = data[paraStartPosition + i]; } // Of for i
/** * My string. String is a class provided by the language, so I use another name. * It is essentially a sequential list with char type elements. * * @author Shihuai Wen Email:wshysxcc@outlook.com. */ publicclassMyString { /** * The maximal length. */ publicstaticfinalintMAX_LENGTH=10;
/** * The actual length. */ int length;
/** * The data. */ char[] data;
/** ********************* * Construct an empty char array. ********************* */ publicMyString() { length = 0; data = newchar[MAX_LENGTH]; }// Of the first constructor
/** ********************* * Construct using a system defined string. * * @param paraString The given string. Its length should not exceed MAX_LENGTH - * 1. ********************* */ publicMyString(String paraString) { data = newchar[MAX_LENGTH]; length = paraString.length();
// Copy data. for (inti=0; i < length; i++) { data[i] = paraString.charAt(i); } // Of for i }// Of the second constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString="";
for (inti=0; i < length; i++) { resultString += data[i]; } // Of for i
return resultString; }// Of toString
/** ********************* * Locate the position of a substring. * * @param paraString The given substring. * @return The first position. -1 for no matching. ********************* */ publicintlocate(MyString paraMyString) { booleantempMatch=false; for (inti=0; i < length - paraMyString.length + 1; i++) { // Initialize. tempMatch = true; for (intj=0; j < paraMyString.length; j++) { if (data[i + j] != paraMyString.data[j]) { tempMatch = false; break; } // Of if } // Of for j
if (tempMatch) { return i; } // Of if } // Of for i return -1; }// Of locate
/** ********************* * Get a substring * * @param paraString The given substring. * @param paraStartPosition The start position in the original string. * @param paraLength The length of the new string. * @return The first position. -1 for no matching. ********************* */ public MyString substring(int paraStartPosition, int paraLength) { if (paraStartPosition + paraLength > length) { System.out.println("The bound is exceeded."); returnnull; } // Of if
MyStringresultMyString=newMyString(); resultMyString.length = paraLength; for (inti=0; i < paraLength; i++) { resultMyString.data[i] = data[paraStartPosition + i]; } // Of for i
return resultMyString; }// Of substring
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { MyStringtempFirstString=newMyString("I like ik."); MyStringtempSecondString=newMyString("ik"); inttempPosition= tempFirstString.locate(tempSecondString); System.out.println("The position of \"" + tempSecondString + "\" in \"" + tempFirstString + "\" is: " + tempPosition);
MyStringtempThirdString=newMyString("ki"); tempPosition = tempFirstString.locate(tempThirdString); System.out.println("The position of \"" + tempThirdString + "\" in \"" + tempFirstString + "\" is: " + tempPosition);
tempThirdString = tempFirstString.substring(5, 6); System.out.println("The substring is: \"" + tempThirdString + "\""); }// Of main } // Of class MyString