This is the 22nd day of my participation in the August Wen Challenge.More challenges in August
The article directories
- Introduction to the
- StringBuilder
- StringBuffer
Introduction to the
When modifying strings, use the StringBuffer and StringBuilder classes.
Unlike the String class, objects in the StringBuffer and StringBuilder classes can be modified multiple times and do not create new unused objects.
StringBuilder
When using the StringBuffer class, the StringBuffer object itself is manipulated each time, rather than generating a new object, so it is recommended to use StringBuffer if you need to modify the string. A StringBuffer is described as follows:
Char [] array (); char[] array ()
Example 1: Large string concatenation
In the process of program development, we often encounter the situation of String concatenation, convenient and direct way is through the + symbol to achieve the purpose, but this way to achieve the efficiency is relatively low, and each execution will create a String object, that is time consuming, and waste space. Using the StringBuilder class avoids this problem
String s0 = "abcdefghigklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder();
long t = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
sb.append(s0);
}
System.out.println(System.currentTimeMillis() - t);
Copy the code
Running results:
You can see that the letters A-Z were concatenated 100,000 times in 22 milliseconds
Create an object
StringBuilder sb = new StringBuilder();
或者
StringBuilder sb = new StringBuilder("abc");
Copy the code
Commonly used method
Append () is used to append characters to the end of a character sequence, efficient string concatenation
Delete (int start,int end) Deletes characters from a string from start to end, that is, [start,end)
DeleteCharAt (int index) Deletes a single character
Insert (int index, substring) Function: Insert a substring at index position
Replace (int start,int end,String STR) Replaces [start,end) with the specified String STR
Reverse () does: reverse
SetCharAt (int index,char ch) replaces the index position with the character CH
ToString () converts toString
Example 2: StringBuilder method exercise
StringBuilder sb = new StringBuilder("abc");
sb.append("def")//abcdef
.append("ghi")//abcdefghi
.insert(2."jkl")//abjklcdefghi
.replace(4.6."mnop")//abjkmnopdefghi
.reverse()//ihgfedponmkjba
.delete(2.5)//ihdponmkjba
.deleteCharAt(0)//hdponmkjba
.setCharAt(2.The '*');//hd*onmkjba
System.out.println(sb);
Copy the code
Running output:
hd*onmkjba
Example 3: Check your hobbies
The following effects need to be achieved:
xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0">
<TableRow>
<CheckBox
android:onClick="doClick"
android:text="Football" />
<CheckBox
android:onClick="doClick"
android:text="Basketball" />
<CheckBox
android:onClick="doClick"
android:text="Balloon" />
</TableRow>
<TableRow>
<CheckBox
android:onClick="doClick"
android:text=Pop music />
<CheckBox
android:onClick="doClick"
android:text="Classical music" />
<CheckBox
android:onClick="doClick"
android:text="Little Moon and moon" />
</TableRow>
<TableRow>
<CheckBox
android:onClick="doClick"
android:text="Beijing Opera" />
<CheckBox
android:onClick="doClick"
android:text="Drama" />
<CheckBox
android:onClick="doClick"
android:text="Cup with" />
</TableRow>
<TableRow>
<CheckBox
android:onClick="doClick"
android:text="Smoking" />
<CheckBox
android:onClick="doClick"
android:text="Drinking" />
<CheckBox
android:onClick="doClick"
android:text="Permed hair" />
</TableRow>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="# 222"
android:textSize="18sp"
android:gravity="center"
android:layout_marginTop="20dp"/>
</TableLayout>
Copy the code
The Activity of
public class MainActivity extends AppCompatActivity {
private TextView textView;
private StringBuilder sb = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
/ * * the View all interface controls the parent type * * | | - EditText - TextView * | -... * * /
public void doClick(View view) {
CheckBox checkBox = (CheckBox) view;
String s = checkBox.getText().toString();
if (checkBox.isChecked()) {/ / check
if(sb.length() ! =0) {
// Add a link to the existing content.
sb.append(",");
}
sb.append(s);
} else {/ / cancel
// Middle case: CCC
//aaa,bbb,ccc,ddd,eee
//start:8 end:12
// At the end: CCC
//aaa,bbb,ccc
//start:8 end:12
//end > 11
// start with CCC
//ccc,aaa,bbb
//start:0 end:4
// Unique: CCC
//ccc
//start:0 end:4
int start = sb.indexOf(s);/ / 8
int end = start + s.length() + 1;//(delete commas, delete is left closed right open range, so add 1)
//end > sb.length
// Delete the last CCC, there is no comma after it, so end--
// Delete CCC as well as the preceding comma, so start--
//
if (end > sb.length()) {
if(start ! =0) { start--; } end--; } sb.delete(start, end); } textView.setText(sb); }}Copy the code
StringBuffer
A StringBuffer is very similar to a StringBuilder, with the following differences:
The StringBuilder class was introduced in Java 5, and the biggest difference between it and StringBuffer is that the Methods of StringBuilder are not thread-safe (they cannot be accessed synchronously).
Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended in most cases.
StringBuilder thread unsafe (high efficiency) StringBuffer thread safe (sacrificing program performance)