Introduction to common Java stringbuffer Methods

Source: Internet
Author: User
Introduction to common StringBuffer Methods
The StringBuffer class is used to process variable strings. It provides a method to modify strings.
(1) constructor:
A. StringBuffer s0 = new StringBuffer (); a 16-byte character buffer is allocated.
B. StringBuffer s1 = new StringBuffer (512); A 512-byte character buffer is allocated.
C. StringBuffer s2 = new StringBuffer ("You are good! ");
Store the string "Happy new year!" In the character buffer! ", In addition, there is a 16-byte empty buffer later.
(2) common methods:
A. Character setting and replacement
SetCharAt (int index, char ch): place the specified ch character at the position indicated by index.
CharAt (int index) gets the character at the specified position
For example:
S = "stedent ";
S. setCharAt (2, "u"), then s is "student"
B. Insert characters
Insert (int offset, char ch), insert the character ch at the offset position.
For example:
StringBuffer s = new StringBuffer ("wecome ");
S. insert (2, 'L'), then s is "welcome"
C. add content at the end of the string (Append method)
StringBuffer s = newStringBuffer ("we ");
Char d = {"l", "c", "o", "m", "e "};
S. append (d); then s is "welcome ".
D. convert to a constant string: toString ().
E. Obtain the length of a string: length ()
StringBuffer s = new StringBuffer ("www ");
Int I = s. length ();
F. Obtain the string capacity.
S. capacity ();
G. Reset the size of the string: ensureCapacity ()
S. ensureCapacity (100 );
H. Set the size of the string buffer.
S. setlength (10 );
If the setlength () method is called with a value less than the current string length, the characters after the new length will be lost.
I. Copy the substring of the string to the array
GetChars (int start, int end, char chars [], int charsStart)
String s1 = "This is a test ";
Int start = 0;
Int end = 10;
Char sequence [] = new char [end-start];
S1.getChars (start, end, begin, 0 );
System. out. println (bytes );
J. String Inversion
S. reverse ();
K. Delete characters in the specified string
Delete (INT start, int end)
S. Delete (0, S. Length (); // Delete All characters of string s
Deletecharat (INT index)
S. deletecharat (4); // Delete the character whose s index is 4
L. Replace string
Replace (INT start, int end, string Str)
S. Replace (0, 1, "qqq ");
M. return part of the string
Substring (INT start) // returns the string starting from the start subscript
Substring (INT start, int end) // returns a string from start to end-1.

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.