Common methods of the String class in Java and the differences between String, StringBuffer, and StringBuilder

1. Know the String class

The String class is in the java.lang package. Java uses the String class to create a string variable, and the string variable belongs to an object. Java declares the final class of the String class and cannot have classes. A String class object cannot be modified after it is created, and consists of 0 or more characters, enclosed between a pair of double quotation marks.

2. Creation of String class objects

1.String str = "Hello";

2.String str = new String("Hello");

3. Common methods of the String class

1.int length(): returns the length of the string

2.char charAt(int index): returns the character at the specified index

3.String toLowerCase(): Convert all characters in the string to lowercase

4.String toUpperCase(): Convert all characters in the string to uppercase

5.String trim(): returns a copy of the string, removes the leading and trailing blanks, and the intermediate blanks will not be removed

6.boolean equals(Object obj): Compare whether the contents of the strings are the same

7. boolean equalsIgnoreCase(String anotherString): Ignore case, compare whether the content of the string is the same String concat(String str): Connect the specified string to the end of this string, which is equivalent to using "+"

8.int compareTo(String anotherString): Compare the size of two strings

9.String substring(int beginIndex): returns the substring from beginIndex to the end

10.String substring(int beginIndex, int endIndex): returns the substring from beginIndex to the previous digit of endIndex, excluding endIndex

11.boolean endsWith(String suffix): Determine whether the string ends with the specified suffix

12.boolean startsWith(String prefix): Determine whether the string starts with the specified prefix

13. boolean startsWith(String prefix, int toffset): Determine whether the substring of the string starting at the specified index starts with the specified prefix

14.boolean contains(CharSequence s): Determine whether the current string contains the specified string

15.int indexOf(String str): Returns the index of the first occurrence of the specified substring in the current string

16.int indexOf(String str, int fromIndex): Returns the index of the first occurrence of the specified substring in the current string after the specified index

17.int lastIndexOf(String str): Returns the index of the last occurrence of the specified substring in the current string

18.int lastIndexOf(String str, int fromIndex): Returns the index of the last occurrence of the specified substring in the current string after the specified index

19.String replace(char oldChar, char newChar): Replace the specified substring in the current string

20.String[] split(String regex): Split the current string according to the specified symbol, and then return a String array

Code example:

public class Main {
    public static void main(String[] args) {
        //方法调用:1.调用方法 对象.方法名()2.传入参数;3.接受方法的返回值
        String s1="asdfg";
        char c=s1.charAt(2);//获取字符中指定索引位置的字符
        System.out.println(c);
        System.out.println("-----------");
        String s2=s1.concat("qwe");//字符串的拼接
        System.out.println(s2);
        System.out.println("-----------");
        boolean b1=s1.contains("qwe");//判断字符串中是否存在某个字符
        boolean b2=s2.contains("qwe");
        System.out.println(b1);
        System.out.println(b2);
        System.out.println("-----------");
        String s3="yueyougong.wwe";
        boolean b3=s3.endsWith(".wwe");//判断文件名的后缀
        System.out.println(b3);
        System.out.println("-----------");
        
        byte[] bytes1={1,2,3,4,5,6};
        String s4=new String(bytes1);//把字符串按照ASCII解析成byte数组
        System.out.println("s4:"+s4);
        System.out.println("-----------");
        byte[] bytes2={1,2,3,4,5,6};
        String s5=new String(bytes2,2,2);//对bytes2数组从索引2开始解析2个长度
        System.out.println(s5);
        System.out.println("-----------");
        char[] char1={'a','b','c','d','e','f'};
        String s6=new String(char1);
        System.out.println("s6:"+s6);
        System.out.println("-----------");
        char[] char2={'a','b','c','d','e','f'};
        String s7=new String(char1,2,2);
        System.out.println("s7:"+s7);
        System.out.println("-----------");


    }
}

4. The difference between String, StringBuffer and StringBuilder

1. String, StringBuffer, and StringBuffer are all final classes and are not allowed to be inherited;

2. Modifying the content of the object declared by String will generate a new object, while StringBuffer and StringBuilder will modify themselves and will not generate new objects;

3. Running speed: StringBuilder > StringBuffer > String

4. Thread safety: StringBuilder < StringBuffer

Note : The biggest difference between String and StringBuilder is that the content of String cannot be modified, but the content of StringBuilder can be modified. Consider using StringBuilder for frequently modifying strings.

Guess you like

Origin blog.csdn.net/AMYCX/article/details/128192279