Common methods of the String class

1. Common methods of the String class

1. startsWith() - to determine whether the string starts with the specified character
2. endWith() - to determine whether the string ends with the specified character
3. substring() - to obtain the substring of the specified position range in the string
4. equals() - compares whether two strings are equal
5. contains() - judges whether the string contains a specified character
6. length() - obtains the length of the string
7. charAt() - obtains Specify the character at the subscript position
8. compareTo()——compare two strings in lexicographical order (ASCII code value subtraction)
9. concant()——splicing the specified string to the end of the string
10. getBytes () - convert the string to bytecode and store it in the specified byte array
11. getChars() - convert the string into characters and store it in the specified character array
12. indexOf() - get the specified character/substring For the subscript that appears for the first time, you can start searching from the specified subscript position
13. isEmpty() - determine whether the length of the string is 0;
14. length() - obtain the length of the string
15. replace() - Replace all specified characters in the string with new characters
16. replaceAll() - replace all specified substrings in the string with new substrings
17. replaceFirst() - replace the string in Replace the first specified substring with a new substring
18. split() ——Split the string with the specified characters and store it in the specified string array
19. toCharArray() - Convert this string to a new character array
20. valueOf() - Convert the specified type to a string type

2. Easy to use

//一、String类的常用方法
public class StringMethod {
    static String str = "张三-李四-王五";
    static String num = "42113132";
    static String ch = "hello,world";
    //字符串方法的操作均不影响原字符串
    public static void main(String[] args) {
//        1. startsWith() ——判断字符串是否以指定字符开头
        boolean s1 = str.startsWith("张");
//        System.out.println(s1);
//        2. endWith() ——判断字符串是否以指定字符结尾
        boolean s2 = str.endsWith("五");
//        System.out.println(s2);
//        3. substring() ——获取字符串中指定位置范围的子字符串
        String s3 = str.substring(0, 3);
//        System.out.println(s3);
//        4. equals() ——比较两个字符串是否相等
        boolean s4 = str.equals("张三");
//        System.out.println(s4);
//        5. contains() ——判断字符串中是否包含某个指定的字符
        boolean s5 = str.contains("李四");
//        System.out.println(s5);
//        6. length() ——获取字符串的长度
        int s6 = num.length();
//        System.out.println(s6);
//        7. charAt() ——获取指定下标位置的字符
        char s7 = ch.charAt(6);
//        System.out.println(s7);
//        8. compareTo()—— 按字典顺序比较两个字符串(ASCII码值相减)
        int s8 = str.compareTo("张三-李四-王五");
//        System.out.println(s8);
//        9. concant() ——将指定的字符串拼接到该字符串的末尾(不影响原字符串)
        String s9 = num.concat("一二三");
//        System.out.println(s9);
//        10. getBytes() ——将字符串转换为字节码存进指定字节数组(不影响原字符串)
        byte[] s10 = ch.getBytes();
//        for (byte b : s10){
//            System.out.println(b);
//        }
//        11. getChars() ——将字符串转换为字符存进指定字符数组(不影响原字符串)
        //getChar(复制起始下标,结束下标,指定字符串,目标数组的起始下标)(复制结果不包含结束下标)
        char[] s11 = new char[ch.length()];
        ch.getChars(0,ch.length(),s11,0);
//        for (char c : s11){
//            System.out.println(c);
//        }
//        12. indexOf() ——获取指定 字符/子字符串 第一次出现的下标,可以从指定下标位置开始搜索
        int s12 = str.indexOf("李四");
//        System.out.println(s12);
//        13. isEmpty() ——判断字符串长度是否为0;
        boolean s13 = num.isEmpty();
//        System.out.println(s13);
//        14. length() ——获取字符串的长度
        int s14 = num.length();
//        System.out.println(s14);
//        15. replace() ——将字符串中所有指定的某个字符替换成新字符(不影响原字符串)
        String s15 = ch.replace('l', 'c');
//        System.out.println(s15);
//        16. replaceAll() ——将字符串中所有指定的某个子字符串替换成新的子字符串(不影响原字符串)
        String s16 = str.replaceAll("李四", "小明");
//        System.out.println(s16);
//        17. replaceFirst() ——将字符串中首个指定的某个子字符串替换成新的子字符串(不影响原字符串)
        String s17 = num.replaceFirst("1", "t");
//        System.out.println(s17);
//        18. split() ——将字符串以指定的字符分割,存放至指定的字符串数组(不影响原字符串)
        String[] s18 = str.split("-");
//        for (String s : s18) {
//            System.out.println(s);
//        }
//        19. toCharArray() ——将此字符串转换为新的字符数组(不影响原字符串)
        char[] s19 = ch.toCharArray();
//        for (char c : s19){
//            System.out.println(c);
//        }
//        20. valueOf() ——将指定类型转换为字符串类型
        char[] cs = {'s','d','d'};
        String s20 = String.valueOf(cs);
//        System.out.println(s20);
//        System.out.println(str);
//        System.out.println(num);
//        System.out.println(ch);
    }
}

Guess you like

Origin blog.csdn.net/yl23921/article/details/127046471