SoFunction
Updated on 2025-03-11

Detailed explanation of how to use Android StringBuffer

Today, let’s talk about the use of StringBuffer.

The StringBuffer class is like String, and is also used to represent strings. However, because the internal implementation of StringBuffer is different from String, StringBuffer does not generate new objects when processing strings, and is better than String class in memory usage.

Therefore, when using it in actual use, if you often need to modify a string, such as insertion, deletion, etc., it is more suitable to use StringBuffer.

There are many methods in the StringBuffer class that are the same as the String class. These methods have the same functions as the functions in the String class.

But there is one of the most significant differences in that each modification of the StringBuffer object will change the object itself, which is the biggest difference from the String class.

In addition, since StringBuffer is thread-safe, there are special chapters to introduce the concept of threads in the future, so it can also be used in multi-threaded programs, but the execution efficiency of the program is relatively slow.

     1. Initialization of StringBuffer object

The initialization of a StringBuffer object is not like the initialization of a String class. Java provides special syntax, and under normal circumstances, it is generally initialized using constructors.

For example:

StringBuffer s = new StringBuffer();

The StringBuffer object initialized in this way is an empty object.

If you need to create a StringBuffer object with content, you can use:

  StringBuffer s = new StringBuffer(“abc”);

The content of the StringBuffer object initialized in this way is the string "abc".

It should be noted that StringBuffer and String are different types and cannot be cast directly. The following code is wrong:

 StringBuffer s = “abc”;        //The assignment type does not match     StringBuffer s = (StringBuffer)”abc”;  //There is no inheritance relationship,Can't make a force

The code for transmuting between the StringBuffer object and the String object is as follows:

String s = “abc”;
     StringBuffer sb1 = new StringBuffer(“123”);
     StringBuffer sb2 = new StringBuffer(s);  //Convert String to StringBuffer     String s1 = ();       //StringBufferConvert toString

     2. Common methods of StringBuffer

The methods in the StringBuffer class mainly focus on changes in strings, such as appending, inserting and deleting, which is also the main difference between StringBuffer and String classes.

a. Append method

  public StringBuffer append(boolean b)

The function of this method is to append content to the end of the current StringBuffer object, similar to a string connection. After calling this method, the content of the StringBuffer object also changes, for example:

  StringBuffer sb = new StringBuffer(“abc”);
          (true);

Then the value of object sb will become "abctrue".

Using this method to connect strings will be more efficient than String, such as connections applied to database SQL statements, such as:

   StringBuffer sb = new StringBuffer();
          String user = “test”;
          String pwd = “123”;
(“select * from userInfo where username=“)
          .append(user)
          .append(“ and pwd=”)
          .append(pwd);

In this way, the value of object sb is a string "select * from userInfo where username=test and pwd=123”

b. deleteCharAt method

public StringBuffer deleteCharAt(int index)

The purpose of this method is to delete the characters at the specified position and then turn the remaining content into a new string. For example:

StringBuffer sb = new StringBuffer(“Test”);
     sb. deleteCharAt(1);

The function of this code deletes the character with an index value of 1 in the string object sb, that is, deletes the second character, and the remaining content forms a new string. So the value of object sb becomes "Tst".

There is also a delete method with similar functions:

public StringBuffer delete(int start,int end)

The purpose of this method is to delete all characters within the specified interval, including start, and not end index value. For example:

 StringBuffer sb = new StringBuffer(“TestString”);
     sb. delete (1,4);

The purpose of this code is to delete all characters between index value 1 (including) and index value 4 (excluding), and the remaining characters form a new string. Then the value of object sb is "TString".

c. Insert method

  public StringBuffer insert(int offset, boolean b)

The function of this method is to insert content into the StringBuffer object and then form a new string. For example:

              StringBuffer sb = new StringBuffer(“TestString”);
                   (4,false);

The purpose of this example code is to insert a false value at the index value 4 of object sb to form a new string, and then the value of object sb is "TestfalseString".

d. Reverse method

   public StringBuffer reverse()

The purpose of this method is to reverse the contents in the StringBuffer object and then form a new string. For example:

  StringBuffer sb = new StringBuffer(“abc”);
     ();

After inversion, the content in object sb will become "cba".

e, setCharAt method                                                           �

  public void setCharAt(int index, char ch)

The function of this method is to modify the character in the object whose index value is index position to the new character ch. For example:

   StringBuffer sb = new StringBuffer(“abc”);
                   (1,'D');

Then the value of object sb will become "aDc".

f, trimToSize method                                                           �

  public void trimToSize()

The purpose of this method is to reduce the storage space in the StringBuffer object to the same length as the string, reducing the waste of space.

In short, when used in actual use, String and StringBuffer have their own advantages and disadvantages. You can select the corresponding type for use according to the specific usage environment.

In addition to the above knowledge, String has three ways of situation content.

StringBuffer sb=new StringBuffer();
(0, ());
(0);

StringBuffer is the most efficient to clear content in a StringBuffer object by using (0);

The above is a detailed explanation of the usage method of Android StringBuffer introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!