为了模仿Java中的String,substring就是在String类中。下面我们来自己实现一下子串的匹配。
一.三个方法模块
locate:找出这个子串开始的索引
public int locate(MyString paraMyString) {
boolean tempMatch = false;
for(int i =0; i<length-paraMyString.length+1;i++) {
tempMatch = true;
for(int j=0;j<paraMyString.length;j++)
{
if(data[i+j]!=paraMyString.data[j]) {
tempMatch = false;
break;
}// Of if
}//Of j
if(tempMatch) {
return i;
}// Of if
}// Of i
return -1;
}// Of locate
substring:从哪里开始,子串长度 例如:substring(2,3)截取从第三个位置开始,长度为3的子串
public MyString substring(int paraStartPosition,int paraLength) {
if(paraStartPosition + paraLength>length) {
System.out.println("The bound is exceeded");
return null;
}// Of if
//创建一个MyString 对象
MyString resultMyString = new MyString();
resultMyString.length = paraLength;
for(int i=0;i<paraLength;i++) {
resultMyString.data[i] = data[paraStartPosition + i];
}// Of for i
return resultMyString;
}
tostring:这个很基础,但很重要。根据自己的需求打印需要的格式。
public String toString() {
String resultString = "";
for (int i = 0; i < length; i++) {
resultString += data[i];
}// Of i
return resultString;
}// Of toString
啰嗦一句:注意算法,比较一个字符串是否属于另一个字符串的子串,两个for:一个for控制循环长度大串-小串+1,第二个则是小串的长度。小串就是参照串。
总体代码入下:
/**
*
*/
package datastructure.list;
public class MyString {
public static final int MAX_LENGTH = 10;
int length;
char[] data;
/**
* 构造一个空的字符数组
*/
public MyString() {
length = 0;
data = new char[MAX_LENGTH];
}// Of construct
public MyString(String paraString) {
data = new char[MAX_LENGTH];
length = paraString.length();
//复制数据
for(int i = 0;i < length; i++) {
data[i] = paraString.charAt(i);
}// Of i
}// Of second constructor
/**
* 重写toString方法
*/
public String toString() {
String resultString = "";
for (int i = 0; i < length; i++) {
resultString += data[i];
}// Of i
return resultString;
}// Of toString
public int locate(MyString paraMyString) {
boolean tempMatch = false;
for(int i =0; i<length-paraMyString.length+1;i++) {
tempMatch = true;
for(int j=0;j<paraMyString.length;j++)
{
if(data[i+j]!=paraMyString.data[j]) {
tempMatch = false;
break;
}// Of if
}//Of j
if(tempMatch) {
return i;
}// Of if
}// Of i
return -1;
}// Of locate
public MyString substring(int paraStartPosition,int paraLength) {
if(paraStartPosition + paraLength>length) {
System.out.println("The bound is exceeded");
return null;
}// Of if
//创建一个MyString 对象
MyString resultMyString = new MyString();
resultMyString.length = paraLength;
for(int i=0;i<paraLength;i++) {
resultMyString.data[i] = data[paraStartPosition + i];
}// Of for i
return resultMyString;
}
//测试函数
public static void main(String args[]) {
MyString tempFirstString = new MyString("I like ik.");
MyString tempSecondString = new MyString("ik");
int tempPosition = tempFirstString.locate(tempSecondString);
System.out.println("The position of \"" + tempSecondString + "\" in \"" + tempFirstString
+ "\" is: " + tempPosition);
MyString tempThirdString = new MyString("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
}