Java.lang.String 类常用方法

来源:互联网 发布:信欣美妆淘宝店假货 编辑:程序博客网 时间:2024/05/21 19:33
        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();


0 0
原创粉丝点击