字符串匹配java

  1. String 是 Java 常用的类, 这里重新实现下部分功能.
  2. 转义符 , 有了它才能正常打印引号.
  3. 简单的越界检查.
/**
 * 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
 */
public class MyString {
	/**
	 * The maximal length.
	 */
	public static final int MAX_LENGTH = 10;
	/* *
	 * The actual length.
	 *  */
	int length;
	/* 
	 * The data
	 *  */
	char data[];
	/*
	 *  Construct an empty char array. 
	 *  */
	public MyString() {
		length=0;
		data = new char[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.
	 *  */
	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
	
	/**
	 *********************
	 * Overrides the method claimed in Object, the superclass of any class.
	 *********************
	 */
	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[i]) {
					tempMatch = false;
					break;
				} // of if
			} // of for j
			
			if(tempMatch) {
				return i;
			}// of if
		} // of for i
		return -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.
	 *********************
	 */
	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);
		
		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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值