2016.3.12 the common methods of StringBuffer class in Java

Source: Internet
Author: User

String is an immutable class, modifying a string using string creates a new string object that, if frequently modified, produces many string objects. It costs a lot. So Java provides a stringbuffer class that is much more efficient at modifying strings than a string.

There are 3 classes in Java that are responsible for the manipulation of characters.

1.Character is a single character operation,

2.String operates on a string of characters. Immutable classes.

3.StringBuffer is also an operation on a string of characters, but a mutable class.

1      Public classUsingstringbuffer {2         /** 3 * Find matching strings4          */  5          Public Static voidTestfindstr () {6StringBuffer SB =NewStringBuffer (); 7Sb.append ("This is a StringBuffer"); 8             //returns the position of the substring that first appears in the string, or a negative number if it does not exist9System.out.println ("Sb.indexof (\" is\ ") =" + Sb.indexof ("is")); Ten             //set a parameter to the IndexOf method, specifying the starting position of the match OneSystem.out.println ("Sb.indexof (\" is\ ") =" + Sb.indexof ("is", 3));  A             //returns the position of the substring in the last occurrence of the string, or a negative number if it does not exist -System.out.println ("Sb.lastindexof (\" is\ ") =" + Sb.lastindexof ("is"));  -             //set a parameter to the LastIndexOf method, specifying the ending position of the match theSystem.out.println ("Sb.lastindexof (\" Is\ ", 1) =" -+ Sb.lastindexof ("is", 1));  -         }   -        +         /**  - * Intercept String +          */   A          Public Static voidTestsubstr () { atStringBuffer SB =NewStringBuffer ();  -Sb.append ("This is a StringBuffer");  -             //The default terminating position is the end of the string -System.out.print ("sb.substring (4) =" + sb.substring (4));  -             //the substring method intercepts a string and can specify the starting and ending positions of the intercept -System.out.print ("sb.substring (4,9) =" + sb.substring (4, 9));  in         }   -        to         /**  + * Get characters from a position in a string -          */   the          Public Static voidTestcharatstr () { *StringBuffer SB =NewStringBuffer ("This is a StringBuffer");  $System.out.println (Sb.charat (Sb.length ()-1)); Panax Notoginseng         }   -        the         /**  + * Add various types of data to the tail of the string A          */   the          Public Static voidTestappend () { +StringBuffer SB =NewStringBuffer ("This is a stringbuffer!");  -Sb.append (1.23f);  $ System.out.println (sb.tostring ());  $         }   -        -         /**  the * Delete data from a string -          */  Wuyi          Public Static voidTestdelete () { theStringBuffer SB =NewStringBuffer ("This is a stringbuffer!");  -Sb.delete (0, 5);  WuSb.deletecharat (Sb.length ()-1);  - System.out.println (sb.tostring ());  About         }   $        -         /**  - * Insert various types of data into the string -          */   A          Public Static voidTestinsert () { +StringBuffer SB =NewStringBuffer ("This is a stringbuffer!");  the             //ability to insert characters, character arrays, strings, and various numbers and Boolean values at a specified location -Sb.insert (2, ' W ');  $Sb.insert (3,New Char[] {' A ', ' B ', ' C ' });  theSb.insert (8, "ABC");  theSb.insert (2, 3);  theSb.insert (3, 2.3f);  theSb.insert (6, 3.75d);  -Sb.insert (5, 9843L);  inSb.insert (2,true);  theSystem.out.println ("Testinsert:" +sb.tostring ());  the         }   About        the         /**  the * Replace some characters in a string the          */   +          Public Static voidTestreplace () { -StringBuffer SB =NewStringBuffer ("This is a stringbuffer!");  the             //Replace a character in a string with another stringBayiSb.replace (Sb.length (), "Integer");  theSystem.out.println ("Testreplace:" +sb.tostring ());  the         }   -        -         /**  the * Reverse the string the          */   the          Public Static voidReversestr () { theStringBuffer SB =NewStringBuffer ("This is a stringbuffer!");  -System.out.println (Sb.reverse ());//reverse method to reverse a string the         }   the}

Summary:
StringBuffer is not an immutable class, and when modifying the contents of a string, no new object is created, so it is more appropriate to modify the string than the string class.
The StringBuffer class does not provide the same string-like ToCharArray method
The Replace method of the StringBuffer class is different from the Replace method of the String class, its replace method has three parameters, the first parameter specifies the starting position of the substituted substring, the second parameter specifies the terminating position of the substituted substring, and the third parameter specifies the new substring

2016.3.12 Java StringBuffer Class common methods in detail (GO)

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.