/**
* 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 Fan Min minfanphd@163.com.
* @learner ChenXun
*/publicclassMyString{/**
* The maximal length.
*/publicstaticfinalint MAX_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(int i=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.
*********************
*/publicStringtoString(){String resultString="";for(int i=0;i<length;i++){
resultString +=data[i];}// of for ireturn 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){boolean tempMatch =false;for(int i=0;i<length-paraMyString.length+1;i++){//Initialize.
tempMatch =true;for(int j=0;j<paraMyString.length;j++){if(data[i+j]!=paraMyString.data[i]){
tempMatch =false;break;}// of if}// of for jif(tempMatch){return i;}// of if}// of for ireturn-1;}// of located/**
*********************
* 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.
*********************
*/publicMyStringsubstring(int paraStartPosition,int paraLength){if(paraStartPosition + paraLength > length){System.out.println("The bound is exceeded.");returnnull;}// Of ifMyString resultMyString =newMyString();
resultMyString.length = paraLength;for(int i=0;i<paraLength;i++){
resultMyString.data[i]=data[paraStartPosition+i];}//of for ireturn resultMyString;}// of subString/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/publicstaticvoidmain(String args[]){MyString tempFirstString=newMyString("I like ik.");MyString tempSecondString =newMyString("ik");int tempPosition = tempFirstString.locate(tempSecondString);System.out.println("The position of \""+ tempSecondString +"\" in \""+ tempFirstString
+"\" is: "+ tempPosition);MyString tempThirdString =newMyString("ki");
tempPosition = tempFirstString.locate(tempThirdString);System.out.println("The position of \""+ tempThirdString +"\" in \""+ tempFirstString
+"\" is: "+ tempPosition);
tempThirdString = tempFirstString.substring(1,2);System.out.println("The substring is: \""+ tempThirdString +"\"");
tempThirdString = tempFirstString.substring(5,5);System.out.println("The substring is: \""+ tempThirdString +"\"");
tempThirdString = tempFirstString.substring(5,6);System.out.println("The substring is: \""+ tempThirdString +"\"");}// of main}// of class MyString