Home > Java > Javagetting Started > Examples to explain common methods of StringBuffer class

Examples to explain common methods of StringBuffer class

王林
Release: 2020-08-04 16:37:48
forward
4409 people have browsed it

Examples to explain common methods of StringBuffer class

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("青春飞扬!");
Copy after login

(2) toString() method: converted to String type

String s=sb.toString();
Copy after login

(3) append() method: append string

字符串.append(参数);
Copy after login

(4) insert() method: insert string

字符串.insert(位置,参数);
Copy after login

(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);
    }
Copy after login

Output result:

Examples to explain common methods of StringBuffer class

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template