String generator

The length of the successfully created string object is fixed, and the content 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 you modify the string repeatedly, it will greatly increase the burden on the system. Using the String-Builder class can greatly improve the efficiency of frequently adding strings.
1. The following examples can be used to verify the efficiency of string operations and string generator operations. Sample code:

public class Demo4 {
	public static void main(String[] args) {
		String str="";
		//定义字符串执行的起始时间
		long starTime=System.currentTimeMillis();
		for (int i = 0; i < 15000; i++) {
			str=str+i;
		}
		long endTime=System.currentTimeMillis();
		long time=endTime-starTime;
		System.out.println("String 运行消耗时间:"+time+"ms");
		StringBuilder builder=new StringBuilder("");
		starTime=System.currentTimeMillis();
		for (int i = 0; i < 15000; i++) {
			builder.append(i);
		}
		endTime=System.currentTimeMillis();
		time=endTime-starTime;
		System.out.println("StringBuilder 运行消耗时间:"+time+"ms");
	}
}

Screenshot of running result:
Insert picture description here
As can be seen from the above example, the time difference between the two operations is very large. If you need to add strings frequently in the program, it is recommended to use StingBuilder. The initial capacity of the newly created StingBuilder object is 16 characters. If the additional characters exceed the acceptable length, the StingBuilder object will automatically increase the length to accommodate the additional characters. When you need
StingBuilder to output the final string result, you can use the toString() method. Use the methods in the StingBuilder class to dynamically perform string operations such as adding, inserting, and deleting.
(1) append() method
This method is used to append content to the string generator. Through multiple overloaded forms of this method, any type of data can be accepted.
Syntax structure: append(content)
(2) insert(int offset, arg) method
This method is used to insert data content into the specified position in the string generator. Offset refers to the position of the string generator. This parameter must be greater than or equal to 0 and less than or equal to the length of this sequence. arg refers to additional data. Sample code:

StringBuilder buil=new StringBuilder("indes");
buil.insert(5, "tructible");
System.out.println(buil.toString());
//运行结果:indestructible

(3) Delete (int start, int end) method
This method is used to remove characters in the substring of this sequence. From the specified start point, until the character at index end-1, if there is no such character, it will continue to the end of the sequence. If start is equal to end, nothing changes. Sample code:

StringBuilder buils=new StringBuilder("Don't forget");
buils.delete(5, 12);
System.out.println(buils.toString());
//运行结果:Don't

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/97147303