Insert character code:
public class Test {
/**Insert new characters in the original characters**/
public static void main(String[] args){
StringBuffer sb = new StringBuffer("Tiantian is a girl!");//Create a character cache area, and the content in the cache area is "Tiantian is a girl!"
("The content in the original character cache is: "+sb);//Output the content in the original character cache
("The length in the original character buffer is: "+() );//Length
("The capacity in the original character cache is: "+() );//Capacity
(5, 'small');// Assign a new value to the value before the specified subscript position
("The content in the new character cache is: "+sb);//Output the content in the new character cache
("The length in the new character cache is: "+() );//Length
("The capacity in the new character cache is: "+() );//Capacity
}
}
explain: (5, ‘small’);// Assign a new value to the value before the specified subscript position, which means inserting the word ‘small’ before the 6th character of sb. 5 is the position of the subscript in the character buffer area, and the same as the array starts from 0.
Running results:
The content in the original character cache area is: Tian Tian is a girl!
The length in the original character cache is: 8
The capacity in the original character cache is: 24
The content in the new character cache is: Tiantian is a little girl!
The length in the new character cache is: 9
The capacity in the new character cache is: 24
/*********************************************************************************************/
/*********************************************************************************************/
Insert string code:
public class Test {
/**Insert new characters in the original characters**/
public static void main(String[] args){
StringBuffer sb = new StringBuffer("Tiantian is a girl!");//Create a character cache area, and the content in the cache area is "Tiantian is a girl!"
("The content in the original character cache is: "+sb);//Output the content in the original character cache
("The length in the original character buffer is: "+() );//Length
("The capacity in the original character cache is: "+() );//Capacity
(5, "Just 18 years old");// Assign a string to the value before the specified subscript position
("The content in the new character cache is: "+sb);//Output the content in the new character cache
("The length in the new character cache is: "+() );//Length
("The capacity in the new character cache is: "+() );//Capacity
}
}
Running results:
The content in the original character cache area is: Tian Tian is a girl!
The length in the original character cache is: 8
The capacity in the original character cache is: 24
The content in the new character cache is: Tiantian is a girl who just turned 18!
The length in the new character cache is: 14
The capacity in the new character cache is: 24
Summarize:
(5, 'small') is to insert the word 'small' before the 6th character of the string sb;
(5, "Just 18 years old"); is to insert the string "Just 18 years old" before the 6th character of the string sb;
Note: Use ‘’, and use ‘’ in strings.