正在试着看jdk源码,看到不错的文章就转了。。。
public final class DString
implements CharSequence, Comparable<DString> {
/*测试*/
public static void main(String[] args){
DString s = new DString("daxi".toCharArray());
System.out.println( s.toString() );
System.out.println( s.charAt(0) );
System.out.println( s.equalsIgnoreCase(" DAXI") );
System.out.println( s.equalsIgnoreCase(
new DString(" DAXI ".toCharArray()).trim() ) );
System.out.println( s.indexOf('x') );
System.out.println( s.startsWith("d", 1) );
System.out.println( s.compareTo( new DString("da".toCharArray()) ) );
/* 输出
* daxi
* d
* false
* true
* 2
* false
* 2
*/
}
public DString() {
this.count_ = 0;
this.value_ = new char[0];
}
public DString(char value[]) {
int size = value.length;
char[] v = new char[size];
System.arraycopy(value, 0, v, 0, size);
this.count_ = size;
this.value_ = v;
}
DString(char value[], int count) {
this.value_ = value;
this.count_ = count;
}
/** 返回此字符串的长度。
* @see java.lang.CharSequence#length()
*/
public int length() {
return count_;
}
public String toString() {
return new String(this.value_);
}
/** 返回指定索引处的 char 值。
* @see java.lang.CharSequence#charAt(int)
*/
public char charAt(int index) {
if ((index < 0) || (index >= count_)) {
throw new StringIndexOutOfBoundsException(index);
}
return value_[index];
}
/** 比较此字符串与指定的对象。
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof DString) {
DString anotherString = (DString)anObject;
int n = count_;
if (n == anotherString.count_) {
char v1[] = value_;
char v2[] = anotherString.value_;
int i = 0;
int j = 0;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
/**将此串与另一个String进行比较,不考虑大小写。*/
public boolean equalsIgnoreCase(String anotherString) {
return (anotherString != null) && (anotherString.length() == count_) &&
regionMatches(true, anotherString, count_);
}
/**按字典顺序比较两个字符串。
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(DString anotherString) {
int len1 = count_;
int len2 = anotherString.length();
int n = Math.min(len1, len2);
char v1[] = value_;
char v2[] = anotherString.value_;
int i = 0;
int j = 0;
if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}
/**测试此字符串是否以指定的前缀开始。该前缀以指定索引开始*/
public boolean startsWith(String prefix, int toffset) {
char ta[] = value_;
int to = toffset;
int po = 0;
int pc = prefix.length();
// Note: toffset might be near -1>>>1.
if ((toffset < 0) || (toffset > count_ - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != prefix.charAt(po++) ) {
return false;
}
}
return true;
}
/**测试此字符串是否以指定的前缀开始。*/
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
/**测试此字符串是否以指定的后缀结束。*/
public boolean endsWith(String suffix) {
return startsWith(suffix, count_ - suffix.length());
}
/**测试两个字符串区域是否相等。*/
public boolean regionMatches(boolean ignoreCase,
String other, int len) {
char ta[] = value_;
char pa[] = other.toCharArray();
int to = 0;
int po = 0;
//Note: len might be near -1>>>1.
if ( ( (long)count_ - len) < 0 ||
( (long)other.length() - len) <0 ) {
return false;
}
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {
// If characters don't match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
/**从指定的索引开始搜索,返回在此字符串中第一次出现指定字符处的索引。*/
public int indexOf(int ch, int fromIndex) {
int max = count_;
char v[] = value_;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= count_) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
int i = fromIndex;
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value_ (invalid code point))
for (; i < max ; i++) {
if (v[i] == ch) {
return i;
}
}
return -1;
}
if (ch <= Character.MAX_CODE_POINT) {
// handle supplementary characters here
char[] surrogates = Character.toChars(ch);
for (; i < max; i++) {
if (v[i] == surrogates[0]) {
if (i + 1 == max) {
break;
}
if (v[i+1] == surrogates[1]) {
return i;
}
}
}
}
return -1;
}
/**返回指定字符在此字符串中第一次出现处的索引。*/
public int indexOf(int ch) {
return indexOf(ch, 0);
}
/**从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。*/
public int indexOf(String str, int fromIndex) {
return indexOf(value_, count_, str.toCharArray(), str.length(), fromIndex);
}
/** 返回一个新的字符序列*/
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count_) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return new String(value_, beginIndex, endIndex - beginIndex);
}
/** 返回一个新的字符序列,它是此序列的一个子序列。
* @see java.lang.CharSequence#subSequence(int, int)
*/
public CharSequence subSequence(int start, int end) {
if (start < 0) {
throw new StringIndexOutOfBoundsException(start);
}
if (end > count_) {
throw new StringIndexOutOfBoundsException(end);
}
if (start > end) {
throw new StringIndexOutOfBoundsException(end - start);
}
return ((start == 0) && (end == count_)) ? this :
new String(value_, 0, end - start);
}
/** 返回字符串的副本,忽略前导空白和尾部空白。*/
public String trim() {
int len = count_;
int st = 0;
char[] val = value_; /* avoid getfield opcode */
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return
((st > 0) || (len < count_)) ? substring(st, len) : this.toString();
}
private int indexOf(char[] source, int sourceCount,
char[] target, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[0];
int max = sourceCount - targetCount;
for (int i = fromIndex; i <= max; i++) {
/* Look for first character. */
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* find first character, now look at the rest of v2 */
if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = 1; j < end && source[j] ==
target[k]; j++, k++);
if (j == end) {
/* find whole string. */
return i;
}
}
}
return -1;
}
/**字符数组值*/
private final char value_[];
/**串中的字符个数*/
private final int count_;
}