Java Fundamentals 12

Source: Internet
Author: User

1 Overview of the StringBuffer1.1 StringBuffer
    • If we need to stitch the strings, each stitch will build a new string object, which is time consuming and wasted space. And stringbuffer can solve this problem.
    • StringBuffer is a thread-safe sequence of characters.
1.2 Construction methods
    • Construction method: No-parameter construction method
 Public StringBuffer ()

    • Construction Method: Specifies the capacity of the string buffer
 Public StringBuffer (int capacity)

    • Constructor method: Specify string contents of a string buffer
 Public StringBuffer (String str)

    • Example:
 PackageCom.xuweiwei; Public classStringbufferdemo { Public Static voidMain (string[] args) {//Public StringBuffer ()StringBuffer SB =NewStringBuffer (); System.out.println ("SB:" +SB); System.out.println ("SB's Capacity:" +sb.capacity ()); System.out.println ("SB's Length:" +sb.length ()); //Public stringbuffer (int capacity)StringBuffer SB2 =NewStringBuffer (50); System.out.println ("SB2:" +SB2); System.out.println ("Capacity of SB2:" +sb2.capacity ()); System.out.println ("Length of SB2:" +sb2.length ()); //Public StringBuffer (String str)StringBuffer SB3 =NewStringBuffer ("Hello"); System.out.println ("SB3:" +SB3); System.out.println ("Capacity of SB3:" +sb3.capacity ()); System.out.println ("Length of SB3:" +sb3.length ()); }}
1.3 StringBuffer Add-on features
    • Method: You can add any type of data to the string buffer and return the string buffer itself, which has overloaded methods
 Public StringBuffer append (String str)

    • Method: Inserts any type of data into the string buffer at the specified location and returns the string buffer itself, which has overloaded methods
 Public StringBuffer Insert (int  offset,string str)

    • Example:
 PackageCom.xuweiwei; Public classStringBufferDemo2 { Public Static voidMain (string[] args) {StringBuffer sb=NewStringBuffer (); //Public stringbuffer Append (String str)Sb.append ("Hello"); Sb.append (true); Sb.append (12); Sb.append (A); System.out.println ("SB:" +SB); Sb.insert (0, ' d '); System.out.println ("SB:" +SB); }}
1.4 StringBuffer Removal function
    • Method: Delete The character at the specified position, note that the deletion is one, and return the string buffer itself
 Public StringBuffer Deletecharat (int index)

    • Method: Deletes the content within the specified range and returns the string buffer itself
 Public StringBuffer Delete (int  start,int end)

    • Example:
 PackageCom.xuweiwei; Public classStringBufferDemo2 { Public Static voidMain (string[] args) {StringBuffer sb=NewStringBuffer (); Sb.append ("Hello"); Sb.append (true); Sb.append (12); Sb.append (A); Sb.insert (0, ' d '); System.out.println ("SB:" +SB); Sb.deletecharat (0); System.out.println ("SB:" +SB); Sb.delete (The); System.out.println ("SB:" +SB); }}
1.5 StringBuffer Replacement function
    • Method: From start to end, replace with STR, return the string buffer object
 Public StringBuffer replace (intint end,string str)

    • Example:
 Package Com.xuweiwei;  Public class StringBufferDemo2 {    publicstaticvoid  main (string[] args) {         New StringBuffer ();        Sb.append ("Hello"). Append ("World"). Append ("Java");        Sb.replace (5, 10, "Hello, World");        System.out.println ("SB:" + SB);    }}
1.6 StringBuffer reversal function
    • Method:
 Public StringBuffer reverse ()

    • Example:
 Package Com.xuweiwei;  Public class StringBufferDemo2 {    publicstaticvoid  main (string[] args) {         New  stringbuffer ();        Sb.append ("Hello"). Append ("World"). Append ("Java");        System.out.println ("SB:" + Sb.reverse ());}    }
interception function of 1.7 stringbuffer
    • Method: Intercepts the content from the specified position, returning the string object
 Public String substring (int start)

    • Method: Truncate from the specified position to the end of the specified position, returning the string object
 Public String substring (int start,int end)

    • Example:
 Package Com.xuweiwei;  Public class StringBufferDemo2 {    publicstaticvoid  main (string[] args) {         New StringBuffer ();        Sb.append ("Hello"). Append ("World"). Append ("Java");        System.out.println ("SB:" + SB);         = sb.substring (3);        System.out.println ("SB:" + SB);        SYSTEM.OUT.PRINTLN ("str:" + str);}            }

Java Fundamentals 12

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.