Use the StringBuffer class to process strings
The StringBuffer class is a reference data type that stores strings more efficiently than the String class.
(Recommended tutorial: java introductory tutorial)
Common methods of the StringBuffer class
(1) Declare the StringBuffer object and initialize
StringBuffer sb=new StringBuffer("青春飞扬!");
(2) toString() method: converted to String type
String s=sb.toString();
(3) append() method: append string
字符串.append(参数);
(4) insert() method: insert string
字符串.insert(位置,参数);
(Video tutorial recommendation: java video tutorial)
Code example:
Common methods for practical application of StringBuffer class
public static void main(String[] args) { StringBuffer sb=new StringBuffer("青春无悔"); int num=110; //在字符串后面追加字符串 StringBuffer sb1=sb.append("我心永恒!"); System.out.println(sb1); //在字符串后面追加整型数字 StringBuffer sb2=sb1.append(520); System.out.println(sb2); //在指定位置插入逗号 StringBuffer sb3=sb2.insert(4,","); System.out.println(sb3); }
Output result:
The above is the detailed content of Examples to explain common methods of StringBuffer class. For more information, please follow other related articles on the PHP Chinese website!