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();
}
}