search
HomeJavaJavagetting StartedExamples to explain common methods of StringBuffer class

Examples to explain common methods of StringBuffer class

Aug 04, 2020 pm 04:37 PM
stringbufferCommon methods

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

(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:

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment