Java String课后作业2

String.equals()方法:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
}

String类方法的使用说明:

1.int Length():

返回字符串长度。

2.char charAt(int n):

返回指定索引处的char值,索引范围为从0到length() - 1. 3.getChars(int srcBegin,int srcEnd, char[] dst,int dstBegin)   

获取从指定位置起的子串复制到字符数组中.要复制的第一个字符位于索引srcBegin处;要复制的最后一个字符位于索引srcEnd-1处(因此要复制的字符总数是srcEnd-srcBegin)。要复制到dst子数组的字符从索引dstBegin处开始,并结束于索引.

 4.String replace(char oldChar,char newChar)子串替换:

返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有oldChar得到的。如果oldChar在此String对象表示的字符序列中没有出现,则返回对此String对象的引用。否则,创建一个新的String对象,它所表示的字符序列除了所有的oldChar都被替换为newChar之外,与此String对象表示的字符序列相同。

5.String toLowerCase():

将此String中的所有字符都转换为小写

 

6.String toUpperCase():

将此String中的所有字符都转换为大写

 

7.String trim():

 去除头尾空格:

 

8.char[] toCharArray():

将字符串对象转换为字符数组

 

原文地址:https://www.cnblogs.com/sakura--/p/7731947.html