Commonly used methods in the String class in Java

    String is mainly used for various operations on strings, and this class is in the java.lang package. The String class is a class modified by the final keyword, so this class cannot be inherited.
Insert picture description here
Initialization of the String class:
    use a string long constant to directly initialize a String object

       String str = "abcde";

    Create a string object according to the constructor of the String class:

       String()//创建一个内容为空的字符串
       String(String value) //根据指定字符串创建对象
       String(char[] value) //根据指定字符数组创建对象

Long-term methods of the String class:

  • int indexOf(int ch): Returns the index of the first occurrence of the specified character in the string
       String str = "abcd";
       System.out.println(str.indexOf("a"));//a在str中第一次出现的位置索引是0
  • int lastIndexOf(int ch): Returns the index of the first occurrence of the specified character in the string
       String str = "abcade";
       System.out.println(str.lastIndexOf("a"));//a在str中最后一次出现的位置索引是3
  • int length(): returns the length of the string
       String str = "abcade";
       System.out.println("字符串的长度:" + str.length());//该字符串的长度是6
  • char CharAt(int index): Returns the character at the index position of the string, index cannot exceed 0-(string length-1)
        String str = "abcade";
        System.out.println("返回索引为1的字符:" + str.charAt(1));
  • boolean startsWith(String prefix): Determine whether the string starts with the specified string
        String str = "abcade";
        System.out.println(str.startsWith("a"));//输出结果为true
        System.out.println(str.startsWith("b"));//输出结果为false
  • boolean endsWith(String prefix): Determine whether the string ends with the specified string
        String str = "abcade";
        System.out.println(str.startsWith("e"));//输出结果为true
        System.out.println(str.startsWith("d"));//输出结果为false
  • boolean equals(Object obj): compare the string with the specified string
        String str = "abcade";
        System.out.println("abc".equals(str));//输出结果为false
        System.out.println("abcade".equals(str));//输出结果为true
  • boolean isEmpty(): Determine whether the string is empty, and return true if and only if the string length is 0
        String str = "abcade";
        System.out.println("字符串是否为空:" + str.isEmpty());//输出为false
  • boolean contains(String str): Determine whether the string contains the specified string sequence
        String str = "abcade";
        System.out.println("该字符串是否包含cade:" + str.contains("cade"));//输出结果为true
  • String toLowerCase(): Convert a string to lowercase characters
        String str = "abcade";
        System.out.println("转换为大写字符:" + str.toUpperCase());
  • String toUpperCase(): Convert a string to uppercase characters
        String str2 = "ASD";
        System.out.println("转换为小写字符:" + str2.toLowerCase());
  • char[] toCharArray(): Convert a string to a character array
        String str = "abcade";
        char[] chars = str.toCharArray();
        //遍历字符数组
        for (char ch: chars){
    
    
            System.out.println(ch);
        }
  • String replace(String oldStr, String newStr): Return a new string, replace oldStr in the string with newStr to get a new string.
        String str = "abcade";
        String replace = str.replace("ab", "dd");
        System.out.println(replace);//输出结果为ddcade
  • char[] split(String regex): Split the string into several substrings according to the parameter regex, which is the interception of the string.
        String str3 = "A-B-C-D";
        String[] split = str3.split("-");
        for (int i=0;i<split.length;i++){
    
    
            if (i != split.length - 1){
    
    
                System.out.print(split[i] + ",");
            }else {
    
    
                System.out.print(split[i]);
            }
        }
  • String substring(int beginIndex), String substring(int beginIndex, int endIndex): Both methods return a new string. The method with only one parameter is to return all characters including the string index beginIndex. The method with two parameters returns the characters from the beginningIndex index to the endIndex.
        String str4 = "abcde";
        System.out.println(str4.substring(3));//输出为cde
        System.out.println(str4.substring(1,3));//输出结果为bc
  • String trim(): Take out the leading and trailing spaces of the original string.
        String str5 = " abcde ";
        System.out.println("去除字符串的前后空格:"str5.trim());//输出的结果abcde不带空格

There are many methods in the String class, and only the commonly used methods are learned here.
Come on! ! !

Guess you like

Origin blog.csdn.net/qq_42494654/article/details/109634054