String的部分源码实现(有一些bug,后期逐渐完善)



import java.util.Arrays;

/**
 * 
 * @author HerbertWuIsReady
 *
 */

public class MyString {
	
	private final char[] value;    
	//MyString在存储上采用的是数组,这个数组采用final修饰,保证了此对象内的string数组只能被初始化一次
	
	private int hash;  //存储此方法的hash值
	
	//构造方法
	
	/**
	 * 无参的构造方法,默认把参数value赋值为空
	 */
	public MyString () {
		value =new char[0];	
	}
	/**
	 * 通关传入Mystring类型的参数,使此类中的MyString的value的值获得参数的value值
	 * @param original
	 */
	public MyString(MyString original) {
        this.value = original.value;
        this.hash = original.hash;
    }
	
	/**
	 * 输入value的字符数组,把value的值用来初始化MyString内的value的值
	 * @param value
	 */
	public MyString(char[] value) {
		this.value = value; 
		this.hash = value.hashCode();
	}
	
	
	public int length() {
		return value.length;
	}
	
    public boolean isEmpty() {
        return value.length == 0;
    }
    
    public char charAt(int index) {
       
    	if ((index < 0) || (index >= value.length)) {
    		//如果发现数组越界,则跑出异常
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }
    
 /*   public int charAt(char c) {
    	
    	int temp =-1;
    	for (int i = 0; i < value.length; i++) {
    		
			if(value[i]==c) {temp =i;break;}
		
    	}
    	return temp;
    }
    
   */
    
    @Override
    public boolean equals (Object obj) {
    	
    	if(obj==null)
    		return false;
    	if(obj instanceof MyString) {
    		if(((MyString) obj).length() != this.length())
    			return false;
    		MyString temp = (MyString)obj;
    		for(int i =0;i<this.length();i++) {
    			if(temp.value[i]!=this.value[i])
    				return false;
    		}
    		return true;
    	}
    	
    	
    	return false;
    }
    
    
    // startwith方法,摘抄自 jdk的String源码
    public boolean startsWith(MyString prefix, int toffset) {
        char ta[] = value;
        int to = toffset;
        char pa[] = prefix.value;
        int po = 0;
        int pc = prefix.value.length;
        // Note: toffset might be near -1>>>1.
        if ((toffset < 0) || (toffset > value.length - pc)) {
            return false;
        }
        while (--pc >= 0) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
        return true;
    }
    
    public boolean endsWith(MyString suffix) {return startsWith(suffix, value.length - suffix.value.length);}
    
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }
    

    public MyString toLowerCase() {
    	char[] temp =this.value;
    	for(int i=0;i<temp.length;i++) {
    		if(temp[i]>=0x41&&temp[i]<='Z')
    			temp[i]+=0x20;
    	}
    	return new MyString(temp);
    }
    public MyString toUpperCase() {
    	char[] temp =this.value;
    	for(int i=0;i<temp.length;i++) {
    		if(temp[i]>=0x61&&temp[i]<='z')
    			temp[i]-=0x20;
    	}
    	return new MyString(temp);
    }
    
    public MyString trim() {
    	char[] temp = this.value;
    	int i=0;
    	int j=temp.length-1;
    	while(temp[i]==' ' || temp[j]==' ') {
    		if(temp[i]==' ')
    			i++;
    		if(temp[j]==' ')
    			j++;
    	}
    	
    	return this.subString(i, j+1);}
    public MyString subString(int begain,int end) {
  
    	return new MyString(    Arrays.copyOfRange(value,begain,end)    );
    	
    }
    public String toString() {return new String(value);}
    
    public char[] toCharArray() {return value;}
    
    public native MyString intern();

    public static void main(String[] args) {
    	char[] c = {' ','b','c','D','e','f','g','h'};
		MyString my = new MyString(c)  ;
		System.out.println(my.subString(1,3));
		System.out.println(my.toLowerCase());
		System.out.println(my.toUpperCase());
		System.out.println();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值