自定义String类

构造初始化

package Part1.string;

public final class MyString {
	
	//定义MyString类的容器
	private char[] value;
	//初始化MyString类
	public MyString() {
		this.value = new char[0];
	}
	public MyString(char[] chars) {
		this.value = copyof(chars);
	}
	public MyString(byte[] bytes) {
		this();
		if(bytes != null) {
			this.value = copyof(bytes);
		}
	}

填充

	//用字符数组填充容器
	private char[] copyof(char[] chars) {
		char[] arr = new char[chars.length];
		for(int i = 0;i < arr.length;i++) {
			arr[i] = chars[i];
		}
		return arr;
	}
	
	//用ASCII值代表的字母填充容器
	private char[] copyof(byte[] bytes) {
		char[] arr = new char[bytes.length];
		for(int i = 0;i < arr.length;i++) {
			arr[i] = (char)bytes[i];//强转为字符
		}
		return arr;
	}

打印

	//以字符串形式打印(传入数组)
	public String tostring(char[] value) {
		String s = "";
		if(this.value == null) {
			return null;
		}else {
			for (char c : this.value) {//c代表该数组中每一个元素 c=value[i]
				s += c;
			}
		}
		return s;
	}
	
	//打印字符串(传入字符串)
	public void printf(MyString s) {
		String ss = "";
		for (int i = 0;i < s.value.length;i++) {//c代表该数组中每一个元素 c=value[i]
			ss += s.charAt(i); 
		}
		System.out.println(ss);
	}

比较字符串各个字符大小

	//比较两个字符串各个字符大小
	public int compareTo(MyString s) {
		if(this == s) {//都为空||相同
			return 0;
		}
		if(this != null && s != null) {
			char[] c1 = this.value;
			char[] c2 = s.value;
			for(int i = 0;i < Math.min(c1.length,c2.length);i++) {
				if(c1[i] != c2[i]) {
					return c1[i] - c2[i];//返回字符值的差
				}
			}
			return Math.abs(c1.length - c2.length);	//字符长度不同,元素相同,返回长度差	
		}else {
			throw new IllegalArgumentException("非空和空不能比较!");//异常
		}
	}


	//忽略大小写比较两个字符串各个字符大小
	public int compareToIgnoreCase(MyString s) {
		if(this == s) {//都为空||相同
			return 0;
		}
		if(this != null && s != null) {
			MyString s1 = change(this);
			MyString s2 = change(s);
			return s1.compareTo(s2);
		}else {
			throw new IllegalArgumentException("非空和空不能比较!");//异常
		}
	}

获取字符串长度

	//获取字符串长度
	public int length(MyString s) {
		return s.getValue().length;
	}

判断字符串是否为空

	//判断字符串是否为空
	public boolean isEmpty(MyString s) {
		if(s.value == null) {
			return true;
		}
		return false;
	}

提取角标对应的元素

	//提取角标对应的元素
	public char charAt(int a) {
		if(a < 0 || a >= this.value.length) {
			System.out.println("角标异常,无法提取");
		}
		return this.value[a];	
	}

提取给定角标间距的字符串[),赋值给给定数组(从角标dstBegin开始赋值)

	//提取给定角标间距的字符串[),赋值给给定数组(从角标dstBegin开始赋值)
	public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
		//数组不为空 && 开始角标>0 && 开始角标<结束角标 && 结束角标 < 数组长度
		if(dst != null && srcBegin > 0 && srcBegin < srcEnd && srcEnd < dst.length) {
			if(dstBegin < 0 || dstBegin >= dst.length) {
				System.out.println("非法参数!");
			}else if(srcEnd - srcBegin > dst.length - dstBegin) {
				System.out.println("非法参数!");
			}
			for(int i = srcBegin;i<srcEnd;i++) {
				dst[dstBegin] = this.value[i];
				dstBegin++;
			}
		}else{
			throw new IllegalArgumentException("非法参数!");
		}
	}

判断字符串是否相等

	//判断字符串是否相等
	public boolean equal(Object obj) {
		//自比
		if(this == obj){
			return true;
		}
		//判断obj是否为MyString类型
		if(obj instanceof MyString){
			MyString s = (MyString) obj;//强转为MyString类型
			return this.compareTo(s) == 0;//调用compareTo()函数,相同返回0 == 0(true)
		}
		return false;
	}
	
	//忽略大小写判断字符串是否相等
	public boolean equalsIgnoreCase(MyString s) {
		//自比
		if(this == s){
			return true;
		}
		MyString s1 = change(this);
		MyString s2 = change(s);
		return s1.equal(s2);
	}
	
	//将字符串中的大写转为小写
	public MyString change(MyString s) {
		//以字符串长度建立字符数组
		char[] c = new char[s.value.length]; 
		for(int i = 0;i < c.length;i++) {
			if(s.charAt(i) >= 65 && s.charAt(i) <= 90) {//遍历找到大写字母
				c[i] = (char) (s.charAt(i) + 32);//转换
			}else {
				c[i] = s.charAt(i);
			}
		}
		return new MyString(c);//以新字符数组创建新字符串
	}
	

截取字符串

	//截取字符串
	public MyString substring(int beginIndex, int endIndex) {
		char[] c = new char[endIndex - beginIndex + 1];
		if(beginIndex < 0 || endIndex > this.value.length - 1) {
			System.out.println("参数不合法!");
		}else if((endIndex - beginIndex) >= this.value.length) {
			System.out.println("参数不合法!");
		}else {
			int index = 0;
			for(int i = beginIndex;i <= endIndex;i++) {
				c[index++] = this.charAt(i);
			}
		}
		return new MyString(c);
	}

新字符代替旧字符

	//新字符代替旧字符
	public MyString replace(char oldChar, char newChar) {
		char[] a = new char[this.value.length];
		for(int i = 0;i < a.length;i++) {
			if(this.charAt(i) == oldChar) {
				a[i] = newChar;
			}else {
				a[i] = this.charAt(i);
			}
		}
		return new MyString(a);
	}

找字符对应的角标

	//找字符对应的角标
	public int indexOf(char c){
		for(int i = 0;i < this.value.length;i++) {
			if(c == this.value[i]) {
				return i;
			}
		}
		return -1;
	}

判断字符串从那个角标开始相等

	//判断字符串从那个角标开始相等
	public int indexOf(MyString s) {
		if(this == s) {
			return 0;
		}
		//0 1 2 3 4
		//2 3 4   即从角标2开始相等
		int i = 0;
		for(;i <= this.value.length - s.value.length;i++) {
			if(this.charAt(i) == s.charAt(0)) {
				MyString m = substring(i,this.value.length - 1);
				if(m.equal(s)) {
					return i;
				}
			}
		}
		return -1;
	}

取消空格符

	//取消空格符
	public MyString trim() {
		int start = 0;
		int end = this.value.length - 1;
		while(start == ' ') {
			start++;
		}
		while(end == ' ') {
			end--;
		}
		//调用截取函数,截断空格
		return substring(start,end);
	}

容器访问器,修改器

//容器访问器,修改器
	public char[] getValue() {
		return value;
	}
	public void setValue(char[] value) {
		this.value = value;
	}
}

测试

package Part1.string;

import java.util.Arrays;

public class testString {
	public static void main(String[] args) {
		//s1 = 
		MyString s1 = new MyString();
		System.out.println(s1.tostring(s1.getValue()));
		//s2 = abcdef
		MyString s2 = new MyString(new char[] {'a','b','c','d','e','f'});
		System.out.println(s2.tostring(s2.getValue()));
		//s3 = abcdefg
		MyString s3 = new MyString(new byte[]{97,98,99,100,101,102,103});
		System.out.println(s3.tostring(s3.getValue()));
		//s1与s2比较,无法进入for循环,返回的是长度差 6
		System.out.println(s1.compareTo(s2));
		//1
		System.out.println(s2.compareTo(s3));
		//System.out.println(s2.compareTo(null));异常情况
		System.out.println(s1.length(s1));//字符串s1的长度  = 0
		System.out.println(s3.length(s3));//字符串s3的长度  = 7
		//s1不为空
		System.out.println(s1.isEmpty(s1));
		//提取角标对应的元素
		System.out.println(s2.charAt(2));// c
		//System.out.println(s2.charAt(8));角标异常,无法提取
		char[] dst=new char[10];
		s3.getChars(1,3,dst,3);
		System.out.println(Arrays.toString(dst));//[ , , , b,c, , , , , ]
		//false
		System.out.println(s2.equal(s3));
		//s4 == s3 (true)
		MyString s4=new MyString(new byte[]{65,98,99,100,101,70,103});
		System.out.println(s3.equalsIgnoreCase(s4));
		//s4 == s3 (0)
		System.out.println(s3.compareToIgnoreCase(s4));
		//bcde
		s2.printf(s2.substring(1,4));
	
		MyString s5 = new MyString(new char[] {'o','p','q','r','s'});
		//opqrs
		s5.printf(s5);
		//wpqrs,不改变原值
		s5.printf((s5.replace('o', 'w')));
		//1
		System.out.println(s5.indexOf('p'));
		//2     从s5的角标2开始相等
		MyString s6 = new MyString(new char[] {'q','r','s'});
		System.out.println(s5.indexOf(s6));
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43498251/article/details/88369608