StringBuffer class StringBuffer Class StringBuffer Class StringBuffer Stop gossiping and return to the truth. Let’s Talk Android!

In today’s chapter, we introduced another string class: StringBuffer, which is one of the most common classes in Java and is often used in real-world projects. Now let’s look at its initialization method.

1. Initialization of StringBuffer

Let’s demonstrate this with pseudocode.

 StringBuffer strBuf1 = "Java";                    / / way
StringBuffer strBuf2 = new   StringBuffer("Java"); 2 / / way
Copy the code

“This looks the same as the initialization of the String class.” This is true, but method one does not comply with Java syntax, so we can only initialize a StringBuffer variable using method two. The reasons are not listed, and we will introduce them in the following chapter.

2. StringBuffer method

Having said that, let’s take a look at the methods that the StringBuffer class gives us to easily manipulate variables of the StringBuffer type.

public int length(a);// Returns the length of the string
public boolean equals(Object arg0);// Determine whether the contents of two strings are equal. Note the difference with the operator "=="
public int indexOf(String arg0);// Find the position of a string, return the position of the string if found, otherwise return -1.
public String substring(int arg0);// Intercepts a string from ar0 to the end of the string
public StringBuffer append(String arg0);// Add the string arg0 to the string to create a new string of type argBuffer
public StringBuffer insert(int arg0,String arg1);// Inserts a new string arg1 at string arg0
public String toString(a);// Convert a StringBuffer variable to a String variable
Copy the code

The StringBuffer type does not support equalsIgnoreCase() and split() methods compared to those provided by String, although it does provide other methods: Insert () and append(). The specific functions of these methods can be seen in the comments above. In addition to these methods, the StringBuffer class provides a number of methods that we won’t list. There are only a few common methods listed here. For the rest you can refer to the JDK documentation. We have listed the prototype functions of common methods and their functions, and we will show you how to use these functions through concrete examples:

public class StringEx {
    public static void main(String args[])
    {
        //StringBuffer strBuf1 = "Java";
        StringBuffer strBuf2 = new StringBuffer("Java");
        StringBuffer strBuf3 = new StringBuffer("JAVA");
        StringBuffer strBuf4 = new StringBuffer("www.google.com");

        System.out.println(strBuf2);

        System.out.println("The length of strBuf2 is: "+strBuf2.length());
        System.out.println("strBuf2 equals Java: "+strBuf2.equals("Java"));
        System.out.println("strBuf2 equals strBuf3: "+strBuf2.equals(strBuf3));
        System.out.println("is google in strBuf4? "+strBuf4.indexOf("google"));
        System.out.println("strBuf4 substring from 4: "+strBuf4.substring(4));  
        System.out.println("strBuf4 append search: "+strBuf4.append("search"));
        System.out.println("strBuf4 insert developper. at 4: "+strBuf4.insert(4."developper."));
        System.out.println("strBuf4 toString: "+strBuf4.toString()); }}Copy the code

The following is the running results of the program, please refer to the code:

Java
The length of strBuf2 is: 4
strBuf2 equals Java: false
strBuf2 equals strBuf3: false
is google in strBuf4? 4
strBuf4 substring from 4: google.com
strBuf4 append search: www.google.comsearch
strBuf4 insert developper. at 4: www.developper.google.comsearch
strBuf4 toString: www.developper.google.comsearch
Copy the code

You can use the above code to analyze the results of the program to better understand the StringBuffer type. Do you have any new findings on this result? In fact, a close observer has spotted an “exception” : the contents of the StringBuffer variable change after executing the append() method of that type. What is the reason for this? We will explain it in detail in the next chapter, as well as in this attentive observer.

StringBuffer class StringBuffer class StringBuffer class StringBuffer