16. String Builder

Table of contents

(1) append() method

(2) insert(int offset, arg) method

(3) delete(int start , int end) method


A successfully created string object has a fixed length and cannot be changed and compiled. Although using "+" can achieve the purpose of appending new characters or strings, "+" will generate a new String instance and create a new string object in memory. If the string is modified repeatedly, it will greatly increase the system overhead

Example 1 : Create class Jerque in the project, write the following code in the main method, and verify the efficiency of string operations and string generator operations

public class Jerque {  //创建类

       public static void main(String[]args) {  //主方法
              String s="";  //定义空字符串
              //定义字符串执行操作的起始时间
              long starTime=System.currentTimeMillis();  //
              for(int i=0;i<10000;i++) {  //利用for循环执行10000次操作
                     s=s+i;  //循环追加字符串
              }
              long endTime=System.currentTimeMillis();  //定义对字符串操作后的时间
              long time=endTime-starTime;  //计算对字符串执行操作的时间
              System.out.println("String消耗时间:"+time);  //将执行的时间输出
              StringBuilder builder=new StringBuilder("");  //创建字符串生成器
              starTime=System.currentTimeMillis();  //定义操作执行前的时间
              for(int j=0;j<10000;j++) {  //利用for循环执行10000次操作
                     builder.append(j);  //循环追加字符
              }
              endTime=System.currentTimeMillis();  //定义操作后的时间
              time = endTime - starTime;  //追加操作执行的时间
              System.out.println("StringBuilder消耗时间:"+time);  //将操作时间输出
       }
}

operation result:

It can be seen from this example that the execution time of the two operations differs greatly. If you frequently append strings in your program, it is recommended to use StringBuilder. The initial capacity of the newly created StringBuilder object is 16 characters, and the initial length can be specified by yourself. If the additional characters exceed the length that can be accommodated, the StringBuilder object will automatically increase the length to accommodate the additional characters. To use StringBuilder to finally output a string result, use the toString() method. Use the methods in the StringBuilder class to dynamically perform string editing operations such as adding, deleting, and inserting. Common methods of this class are as follows.

(1) append() method

This method is used to append content to the string builder. Through multiple overloaded forms of this method, any type of data can be accepted, such as int, boolean, char, String, double or another string generator.

The syntax is as follows:

Among them, content represents the content to be appended to the string generator, which can be any type of data or other objects.

(2) insert(int offset, arg) method

This method is used to insert data content into the specified position in the string builder. Through different overloaded forms of this method, basic data types such as int, float, char and boolean or other objects can be inserted into the string generator.

The syntax is as follows:

  • offset: The position of the string builder. This parameter must be greater than or equal to 0 and less than or equal to the length of this sequence.
  • arg: The position to insert into the string builder. The parameter can be any data type or other object

Example 2 : Add characters to the position specified in the string builder

(3) delete(int start , int end) method

Removes characters from substrings of this sequence. The substring starts at the specified start and continues to the character at index end -1, or to the end of the sequence if no such character exists. If start is equal to end, no change occurs.

The syntax is as follows:

  • start: The starting position of the string to be deleted.
  • end: The end position of the string to be deleted.

Example 3 : Delete the substring at the specified position

Guess you like

Origin blog.csdn.net/qq_25990967/article/details/128777839