Java第17天——字符串匹配

1 String 是 Java 常用的类, 这里重新实现下部分功能.
2 转义符 , 有了它才能正常打印引号.

3 简单的越界检查.

搜索到了字符串的匹配匹配算法

BF算法 和KMP算法

代码

package datastructure.list;
/**
 * @author goudiyuan
 */

public class MyString {
	
	//字符最大长度
	public static final int MAX_LENGTH = 10;
	//实际长度
	int length;
	//存放数据的地方
	char[] data;
	//无参构造方法
	public MyString() {
		length = 0;
		data = new char[MAX_LENGTH];
	}
	//有参构造方法
	public MyString(String paraString) {
		data = new char[MAX_LENGTH];
		length = paraString.length();

		//将字符串拆分存入数组
		for (int i = 0; i < length; i++) {
			data[i] = paraString.charAt(i);
		} 
	}

	//重写父类toString方法
	public String toString() {
		String resultString = "";

		for (int i = 0; i < length; i++) {
			resultString += data[i];
		} 

		return resultString;
	}

	//找到子字符串的位置
	//返回第一个位置。-1 表示不匹配
	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;
				}
			}

			if (tempMatch) {
				return i;
			}
		}
		return -1;
	}

	//获取子字符串,相当于重写了截取字符串方法substring(int,int),但有区别
	//返回第一个位置。-1 表示不匹配
	//paraLength表示要截取的字符串长度
	public MyString substring(int paraStartPosition, int paraLength) {
		if (paraStartPosition + paraLength > length) {
			System.out.println("超出限制.");
			return null;
		}

		MyString resultMyString = new MyString();
		resultMyString.length = paraLength;
		for (int i = 0; i < paraLength; i++) {
			resultMyString.data[i] = data[paraStartPosition + 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("字串 \"" + tempSecondString + "\"在字符串 \"" + tempFirstString
				+ "\" 的位置是: " + tempPosition);

		MyString tempThirdString = new MyString("ki");
		tempPosition = tempFirstString.locate(tempThirdString);
		System.out.println("字串\"" + tempThirdString + "\"在字符串 \"" + tempFirstString
				+ "\" 的位置是: " + tempPosition);

		tempThirdString = tempFirstString.substring(1, 2);
		System.out.println("截取的子字符串是: \"" + tempThirdString + "\"");

		tempThirdString = tempFirstString.substring(5, 5);
		System.out.println("截取的子字符串是: \"" + tempThirdString + "\"");

		tempThirdString = tempFirstString.substring(5, 6);
		System.out.println("截取的子字符串是: \"" + tempThirdString + "\"");
	}
}

运行结果

字串 "ik"在字符串 "I like ik." 的位置是: 3
字串"ki"在字符串 "I like ik." 的位置是: -1
截取的子字符串是: " l"
截取的子字符串是: "e ik."
超出限制.
截取的子字符串是: "null"

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值