The
Creates a successful string object whose length is fixed and cannot be modified and edited. Although you can use "+" to add new characters or strings, "+" produces a new instance of string that creates new strings objects in memory. If you repeatedly modify the string, you will greatly increase the overhead of the system. J2SE has increased the String-builder class of variable character sequences from 5.0, greatly increasing the efficiency of increasing strings frequently. Let's look at a simple example.
public class Jerque {
/**
* Comparison of generic string generators, and string-builder elapsed time *
* public
static void Main (string[) args) {
//TODO auto-generated method stub
String str = "";
Long startTime1 = System.currenttimemillis ();
for (int i =0; i<10000; i++)
{
str = str +i;
}
Long endTime1 = System.currenttimemillis ();
Long time1 = endtime1-starttime1;
System.out.println ("String 1 Consumes Time:" + time1);
StringBuilder builder = new StringBuilder ("");
Long startTime2 = System.currenttimemillis ();
for (int i=0;i<10000;i++)
{
builder.append (i);
}
Long endTime2 = System.currenttimemillis ();
Long time2 = endtime2-starttime2;
System.out.println ("String 2 consumes Time:" + time2);
}
String 1 consumption time: 1210
String 2 consumption time: 3
Summarize:
1. The String-builder class with Java-brought character sequences undoubtedly greatly improves the efficiency.
2. Such commonly used methods are as follows:
A. Append (content) method
B. Insert (int offset,arg) method
S Tringbuilder B = new StringBuilder ("Hello");
B.insert (5, "world!");
System.out.println (b.tostring ());
C. Delete (int start,int end) method
& nbsp StringBuilder d = new StringBuilder ("StringBuilder");
d.delete (5,10);
System.out.println ("d.tostring ()");