[real time update] learning of String and stringbuild methods (the method is simple and suitable for novices)

Posted by libertyct on Sun, 02 Jan 2022 21:34:13 +0100


The object of String is immutable, StringBuffer and The object of StringBuilder is variable, and StringBuilder is an improved class of StringBuilder. The methods of the two are similar. Therefore, we mainly record the methods in learning String and stringbuild classes.

Methods in String

1.intern() translation address

There are no parameters in this method. It is used to point the address of the variable to the constant pool. When the assignment of a variable is not all constants (""), the variable points to heap memory, and the use of intern() can make the variable point to the constant pool.

Example:

        String s1 = "hello";
        String s2 = "world";
        String s3 = "hello" + "world";
        String s4=s1+"world";
        String s5=s1+s2;
        String s6=(s1+s2).intern();

        System.out.println(s3==s4);//false
        System.out.println(s3==s5);//false
        System.out.println(s4==s5);//false
        System.out.println(s3==s6);//true

Both s3 and s6 are "helloworld", but s3 points to the constant pool, s6 is an object before intern, and the use of the intern method makes s6 point to the constant pool.

2.split(String a) split

There are parameters in this method. The parameters are of String type. Take the parameters as the dividing line, divide the String into strings, and return the String array.

Example:

        String s="hello,world,hello,java";
        String[] ss=s.split(",");
        
        System.out.print(ss[0]+" ");
        System.out.print(ss[1]+" ");
        System.out.print(ss[2]+" ");
        System.out.print(ss[3]+" ");
        //hello world hello java

Use "," as the separator to separate strings.

3.substring() interception

There are parameters in this method. The parameters are of type int. there are two kinds of parameters: (1) there is a parameter. The parameter is the index value. Intercept the string starting with the parameter as the index value and return it to the object. (2) There are two parameters, one is the index value of the initial position, and the other is the index value of the last bit after the end value. Intercept the string between the segments and return it to the object.

Example:

        String s="hello world";
        String ss=s.substring(0,5);
        //hello

The above figure intercepts the string from bit 1 to bit 5 and returns it to ss.

4. Convert touppercase() to uppercase

This method has no parameters and is used to convert all characters of String to uppercase letters

Example:

        String s="hello world";
        String ss=s.toUpperCase();
        //HELLO WORLD


5.trim() remove spaces

This method has no parameters and is used to remove the spaces on the left and right sides of the string

Example:

        String s="  hello world  ";
        String ss=s.trim();
        //hello world


Methods in StringBuilding

1.append() add

This method has parameters of any type. Add parameters to the object.

Example:

        StringBuilder s=new StringBuilder();
        s.append("hello");
        System.out.println(s);
        //hello

        s.append(2021);
        System.out.println(s);
        //hello2021


2. Reverse

This method has no parameters and returns the opposite sequence of parameters.

Example:

        StringBuilder s=new StringBuilder();
        s.append("hello world");
        s.reverse();
        System.out.println(s);
        //dlrow olleh


3. Length

This method has no parameters, returns the length of the object and stores the actual value

Example:

        StringBuilder s=new StringBuilder();
        s.append("hello world");
        System.out.println(s.length());
        //11


4.toString() conversion

This method has no parameters and returns String type. Through this method, you can convert StringBuilder type to String type.

Example:

        StringBuilder s=new StringBuilder();
        s.append("hello world");
        String ss=s.toString();
        System.out.println(ss);
        //hello world

The type of S is originally StringBuilder, and the type of ss is String. Use the toString method to assign the value of s to ss.

Topics: Java string