Java 12-4 StringBuffer class substitution, inversion, interception function

Source: Internet
Author: User

1, the replacement function of StringBuffer:
Public stringbuffer replace (int start,int end,string str): Replace with str from start to end

1  Public classStringbufferdemo {2  Public Static voidMain (string[] args) {3 //creating a String buffer object4StringBuffer SB =NewStringBuffer ();5 6 //Add Data7Sb.append ("Hello");8Sb.append ("World");9Sb.append ("Java");TenSystem.out.println ("SB:" +SB); One  A //Public stringbuffer replace (int start,int end,string - //str): Replace with str from start to end - //need: I want to replace the world data with "Happy Holidays" theSb.replace (5, 10, "Happy Holidays"); -System.out.println ("SB:" +SB); - } -}


2, StringBuffer reversal function:
Public StringBuffer Reverse ()

1  Public classStringbufferdemo {2  Public Static voidMain (string[] args) {3 //creating a String buffer object4StringBuffer SB =NewStringBuffer ();5 6 //Add Data7Sb.append ("Xia Qing lin Love Me");8System.out.println ("SB:" +SB);9 Ten //Public StringBuffer Reverse () One sb.reverse (); ASystem.out.println ("SB:" +SB); - } -}


3, StringBuffer interception function: Note The return value type is no longer the stringbuffer itself, but the string
Public String substring (int start)
Public String substring (int start,int end)

1  Public classStringbufferdemo {2  Public Static voidMain (string[] args) {3 //creating a String buffer object4StringBuffer SB =NewStringBuffer ();5 6 //adding elements7Sb.append ("Hello"). Append ("World"). Append ("Java");8System.out.println ("SB:" +SB);9 Ten //interception function One //Public String substring (int start) AString s = sb.substring (5); -System.out.println ("s:" + s);//Worldjava -System.out.println ("SB:" + SB);//Helloworldjava, because the above returns a string, not itself, so it does not change the  - //Public String substring (int start,int end) -String SS = sb.substring (5, 10); -SYSTEM.OUT.PRINTLN ("SS:" + ss);// World +System.out.println ("SB:" + SB);//Helloworldjava - } +}

Java 12-4 StringBuffer class substitution, inversion, interception function

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.