Java.lang.String 類常用方法

        String 類是 Java 中非常常用的類,如果能熟練掌握其方法,那編程速度會大大提升。

一. 構造函數
public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}
public String(char value[], int offset, int count) {
    ......
}
Ps:offset:偏移量(從數組的這裏開始截取字符串),count:要幾個字符。

二. 取得字符串屬性
1. 獲取字符串長度。
public int length() {
    return value.length;
}

2.  判斷是否爲空。
public boolean isEmpty() {
    return value.length == 0;
}

3. 字符串比較
public int compareTo(String anotherString) {
    ......
}
Ps:如果兩個字符串相等,那麼返回0;如果這個String 比參數的String 要小,那麼返回 一個負數;如果這個String 比參數的String 要大,那麼返回一個整數。

4. 比較字符串是否相等(忽略大小寫)
public boolean equalsIgnoreCase(String anotherString) {
    return (this == anotherString) ? true
    : (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}

5. 判斷字符串是否以固定的字符串固定的位置開始
public boolean startsWith(String 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;
}

6. 判斷是否以固定字符串開頭
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}

6. 判斷是否以固定字符串結尾
public boolean endsWith(String suffix) {
    return startsWith(suffix, value.length - suffix.value.length);
}

7. 返回特定字符串第一次出現的位置
public int indexOf(String str) {
    return indexOf(str, 0);
}
8. 返回特定字符串最後一次出現的位置
public int lastIndexOf(String str) {
    return lastIndexOf(str, value.length);
}

9. 截取固定的字符串(從參數開始--到最後)
public String substring(int beginIndex) {
    if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}

10. 截取 index1 到 index2 固定的字符串(從參數開始--到最後)
public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}

11.連接(把參數字符串連接到調用者字符串下)
public String concat(String str) {
    int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}

12. 替換
public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
 
while (++i < len) {
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(buf, true);
}
}
return this;
}

13. 分割函數
public String[] split(String regex) {
return split(regex, 0);
}

14.大小寫轉換
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}

15.去除前後空格
public String trim() {
int len = value.length;
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 < value.length)) ? substring(st, len) : this;
}

16.轉換爲字符數組
public char[] toCharArray() {
    // Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}

17. valueOf() 相當於構造函數
public static String valueOf(char data[]) {
    return new String(data);
}
public static String copyValueOf(char data[], int offset, int count) {
    return new String(data, offset, count);
}
public static String copyValueOf(char data[]) {
return new String(data);
}

18.把字符串加入到常量池中
public native String intern();


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章