Getting Started with Java--stringbuffer class

Source: Internet
Author: User

Getting Started with Java--stringbuffer class Recognize the StringBuffer class

If a string needs to be changed frequently, you need to use the StringBuffer class. (A variable of type string can be difficult to change once it is declared, and if you want to change it, you must change the reference address)!

Connection operation for Strings

Use the Append method in the program book to connect the strings.

Package Sep22;public class StringBufferDemo01 {public static void main (string[] args) {StringBuffer buf=new stringbuffer ( ); Buf.append ("Hello");//use Append to add content buf.append ("world!"). APPEND ("!!!!"); /Continuous Add content buf.append ("\ n"); Buf.append ("Number:"). Append (1). Append ("\ n"); System.out.println (BUF);}}

  

Hello WORLD!!!!! Numbers: 1

  

Add content to StringBuffer at any location

You can use the Insert () method directly.

Package Sep22;public class StringBufferDemo03 {public static void main (string[] args) {StringBuffer buf=new stringbuffer ( ); Buf.append ("wodld"); Buf.insert (0, "Hello"); System.out.println (BUF);}}
Hello WODLD
String inversion operation

  

Getting Started with Java--stringbuffer class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.