第 19 天: 字符串匹配
字符串匹配原理简单,但是对于代码有一点疑问,主要是基础的不牢固,对语法的理解不太行,核心思想没有什么问题。先上今天打的代码。
package day17;
public class MyString {
	public static final int MAX_LENGTH = 10;
	char[] data;
	int length;
	public MyString() {
		length = 0;
		data = new char[MAX_LENGTH];
	}// Of the first constructor
	public MyString(String paraString) {
		data = new char[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
	public String toString() {
		String resultString = "";
		for (int i = 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.
	 *********************
	 */
	public int locate(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[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.");
			return null;
		} // Of if
		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;
	}// Of substring
	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	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);
		System.out.println(tempSecondString.length);
		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
	
	
}// Of class MyString 
当时对于locate方法的外层for循环条件不理解,觉得这个length并没有指明到底是哪个length,然后反复看了下第二个构造器和主方法的调用,理解了这个length就是你最开始new的那个MyString的长度。然后又觉得不对了,觉得不该+1,结果自己一把字符串假设出来模拟发现需要+1。
然后就是入口的输出内容,对于转义符这里做一个总结:
|   转义字符  |   意义  |   ASCII码值(十进制)  | 
|   \b  |   退格(BS) ,将当前位置移到前一列  |   008  | 
|   \f  |   换页(FF),将当前位置移到下页开头  |   012  | 
|   \n  |   换行(LF) ,将当前位置移到下一行开头  |   010  | 
|   \r  |   回车(CR) ,将当前位置移到本行开头  |   013  | 
|   \t  |   水平制表(HT) (跳到下一个TAB位置)  |   009  | 
|   \v  |   垂直制表(VT)  |   011  | 
|   \\  |   代表一个反斜线字符''\'  |   092  | 
|   \'  |   代表一个单引号(撇号)字符  |   039  | 
|   \"  |   代表一个双引号字符  |   034  | 
|   \0  |   空字符(NULL)  |   000  | 
|   \ddd  |   1到3位八进制数所代表的任意字符  |   三位八进制  | 
|   \uhhhh  |   1到2位十六进制所代表的任意字符  |   二位十六进制  | 
总的来说,对于构造器的理解不够,还有些语法,书写习惯有问题,但是对于代码的核心思想还是掌握了,需要更多书写。










