当前位置:网站首页>Common methods in string class

Common methods in string class

2022-07-04 22:50:00 crazyK.

Method  

charAt(int index) return char Specify the value at the index .
codePointAt(int index) Returns the character at the specified index (Unicode Code points )
codePointBefore(int index) Returns the characters before the specified index (Unicode Code points ).
concat(String str) Connect the specified string to the end of the string .
contains(CharSequence s) If and only if the string contains the specified char Value sequence true.
endsWith(String suffix) Tests whether the string ends with the specified suffix .
equals(Object anObject) Compare this string with the specified object .
hashCode() Returns the hash code of this string
indexOf(String str, int fromIndex) Returns the index within the string of the first occurrence of the specified substring , Start the search with the specified index . formIndex Can not pass
isEmpty() return true If , And only if length() by 0 .
lastIndexOf(String str, int fromIndex) Returns the index of the last occurrence of the specified substring , Search backwards from the specified index . formIndex Can not pass
length() Returns the length of this string
replace(char oldChar, char newChar) Returns a string that results from replacing all occurrences oldChar In this string newChar
replace(CharSequence target, CharSequence replacement) Replaces each substring of the string that matches the literal target sequence with the specified literal replacement sequence .
split(String regex) Splits this string into the given regular expression Of matching .
startsWith(String prefix) Tests whether the string starts with the specified prefix .
substring(int beginIndex, int endIndex) Returns a string , This string is a substring of this string . endIndex Can not pass
toCharArray() Convert this string to a new character array .
toLowerCase() Put all characters here String Rules for using the default locale , In lowercase .
toUpperCase() Put all characters here String Use the default locale's rules for capitalization .

Use samples  

public class StringTest {
    private static String str = "1,a,2,b,3,c";

    public static void main(String[] args) {
        System.out.println("charAt: "+ str.charAt(0));
        System.out.println("codePointAt: " + str.codePointAt(0));
        System.out.println("codePointBefore: " + str.codePointBefore(3));
        System.out.println("concat: " + str.concat(","));
        System.out.println("contains: " + str.contains(","));
        System.out.println("endsWith: " + str.endsWith(","));
        System.out.println("equals: " + str.equals("ss"));
        System.out.println("hashCode: " + str.hashCode());
        System.out.println("indexOf: " + str.indexOf(",", 2));
        System.out.println("isEmpty: " + str.isEmpty());
        System.out.println("lastIndexOf: " + str.lastIndexOf(","));
        System.out.println("length: " + str.length());
        System.out.println("replace1: " + str.replace("a", "A"));
        System.out.println("replace2: " + str.replace("a,2,b", "A,2,B"));
        String[] split = str.split(",");
        for (String s : split) {
            System.out.print(s);
        }
        System.out.println();
        System.out.println("startsWith: " + str.startsWith("1"));
        System.out.println("substring: " + str.substring(3, 5));
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            System.out.print(aChar);
        }
        System.out.println();
        System.out.println("toUpperCase: " + str.toUpperCase());
        System.out.println("toLowerCase: " + str.toUpperCase().toLowerCase());
    }
}

Console printing

charAt: 1
codePointAt: 49
codePointBefore: 97
concat: 1,a,2,b,3,c,
contains: true
endsWith: false
equals: false
hashCode: 713949408
indexOf: 3
isEmpty: false
lastIndexOf: 9
length: 11
replace1: 1,A,2,b,3,c
replace2: 1,A,2,B,3,c
1a2b3c
startsWith: true
substring: ,2
1,a,2,b,3,c
toUpperCase: 1,A,2,B,3,C
toLowerCase: 1,a,2,b,3,c

原网站

版权声明
本文为[crazyK.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042221378786.html