package com.tmx.string;
public class MyString implements java.io.Serializable {
private static final long serialVersionUID = -1597485086838057214L;
/**
* 字符数组,私有最终变量,只能赋值一次
*/
private final char[] value;
/**
* 构造空串"",串长度为0
*/
public MyString() {
this.value = new char[0];
}
/**
* 由字符串常量str构造串,复制拷贝的过程
* @param str
*/
public MyString(java.lang.String str) {
this.value = new char[str.length()];
for (int i = 0; i < this.value.length; i++) {
this.value[i] = str.charAt(i);
}
}
/**
* 以字符数组value的i开始的count个字符构造串,i>=0,count>=0,i+count<=value.length
* @param value
* @param i
* @param count
*/
public MyString(char[] value, int i, int count) {
if (i >= 0 && count >= 0 && i + count <= value.length) {
this.value = new char[count];
for (int j = 0; j < count; j++) {
this.value[j] = value[i + j];
}
} else {
throw new StringIndexOutOfBoundsException("i=" + i + ",count=" + count + ",i+count=" + (i + count));
}
}
/**
* 以整个字符数组构造串
* @param value
*/
public MyString(char[] value) {
this(value, 0, value.length);
}
/**
* 以byte数组bytes的一部分构造串
* @param bytes
* @param begin
* @param length
*/
public MyString(byte[] bytes, int begin, int length) {
if (begin >= 0 && length >= 0 && begin + length <= bytes.length && begin <= length) {
this.value = new char[length];
for (int i = begin; i < length; i++) {
this.value[i] = (char) (bytes[i]);
}
} else {
throw new StringIndexOutOfBoundsException( "begin=" + begin + ",length=" + length + ",begin+length=" + (begin + length));
}
}
/**
* 重载构造方法
* @param bytes
*/
public MyString(byte[] bytes) {
this(bytes, 0, bytes.length);
}
/**
* 深度拷贝,复制数组
* @param str
*/
public MyString(MyString str) {
this(str.value);
}
/**
* 串长度即为字符数组的长度
* @return
*/
public int length() {
return this.value.length;
}
public java.lang.String toString() {
return new String(this.value);
}
/**
* 返回第index个字符,0<=index<length();
* @param index
* @return
*/
public char charAt(int index) {
if (index >= 0 && index < this.length()) {
char cha = this.value[index];
return cha;
} else {
throw new StringIndexOutOfBoundsException("index=" + index);
}
}
/**
* 判断两个串是否相等
* @param str
* @return
*/
public boolean equals(java.lang.String str) {
boolean flag = false;
if (this.value.length != str.length()) {
flag = false;
} else {
for (int i = 0; i < str.length(); i++) {
if (this.value[i] != str.charAt(i)) {
flag = false;
break;
} else {
flag = true;
}
}
}
return flag;
}
/**
* 返回序号从begin--(end-1)的子串
* @param begin
* @param end
* @return
*/
public MyString substring(int begin, int end) {
if (begin >= 0 && end <= this.length() && begin <= end) {
if (begin == 0 && end == this.length()) {
return this;
} else {
return new MyString(this.value, begin, end - begin);
}
} else {
throw new StringIndexOutOfBoundsException("begin=" + begin + ",end=" + end);
}
}
/**
* 重载
* @param begin
* @return
*/
public MyString substring(int begin) {
return this.substring(begin, this.length());
}
/**
* 从指定位置开始查找指定的字符的位置,没有返回-1
* @param cha
* @param begin
* @return
*/
public int indexOf(char cha, int begin) {
int j = 0;
for (int i = begin; i < this.value.length; i++) {
if (cha == this.value[i]) {
j = i;
break;
} else {
j = (-1);
}
}
return j;
}
/**
* 重载indexOf方法,从头开始查找
* @param cha
* @return
*/
public int indexOf(char cha) {
return this.indexOf(cha, 0);
}
/**
* 查找指定的字符串的位置,未找到返回-1
* @param str
* @return
*/
public int indexOf(java.lang.String str) {
return this.indexOf(str, 0);
}
/**
* 从指定的位置开始查找指定的串位置
* @param str
* @param begin
* @return
*/
public int indexOf(java.lang.String str, int begin) {
int j = 0;
int strLength = str.length();
if (begin < 0) {
begin = 0;
}
if (strLength > this.length() || strLength + begin > this.length() || begin > this.length()) {
j = (-1);
} else {
int beginInt = this.indexOf(str.charAt(0), begin);
if (beginInt == (-1)) {
j = (-1);
} else {
MyString str1 = this.substring(beginInt, beginInt + strLength);
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == str.charAt(i)) {
j = beginInt;
} else {
j = (-1);
}
}
}
}
return j;
}
}
JAVA字符串String的设计
最新推荐文章于 2023-03-07 18:50:46 发布