Java string and StringBuffer classes

Source: Internet
Author: User
Tags integer lowercase stringbuffer

Here's a summary of the methods that apply to both string and stringbuffer in order to have an impression of how they communicate with each other. These tables do not include each individual method, but rather include methods that are relevant to this discussion. The methods that have been overridden are summarized in a single line.
First, summarize the various methods of the String class:

Method independent variable, covering use

The builder has been overridden: default, String,stringbuffer,char array, byte array create String Object
Length () No number of characters in string
CharAt () int Index Char at a location within a string
GetChars (), getBytes the beginning and end of the copy, the array to which to copy the content, and one index of the target array to copy char or byte inside the outer array
ToCharArray () does not produce a char[] that contains the characters inside the string
Equals (), Equalsignorecase () is used to compare the contents of a string of two strings for equivalence checking
CompareTo () is used to contrast a string result that is negative, 0, or positive, depending on the dictionary order of the string and the argument. Note that uppercase and lowercase are not equal!
Regionmatches () The position offset of this string and other strings, and the length of the area to compare. Overrides a Boolean result that adds a "Ignore case" attribute, indicating whether the area to be compared is the same
StartsWith () A string that may start with it. Overlay in the argument adds an offset Boolean result that indicates whether the string starts with that argument.
EndsWith () may be a Boolean result of a string of this string suffix, indicating whether the argument is not a suffix
IndexOf (), LastIndexOf () overridden: Char,char and start index, string,string and start index if the argument is not found in this String, return 1; otherwise, return the index of the position at the beginning of the variable. LastIndexOf () to start a backtracking search from the end point
SUBSTRING () Overridden: Start index, start index, and end index returns a new string object that contains a specified subset of characters
Concat () The string that you want to link returns a new string object that contains the characters of the original string, followed by the characters provided by the argument
Relpace () The old character to look for, the new character to replace with it returns a new string object in which the replacement work has been completed. If you don't find a matching search term, use the old string.
toLowerCase (), toUpperCase () does not return a new string object, in which all the uppercase and lowercase characters are unified. If you do not need to modify the old string
Trim () does not return a new string object, and the ends and ends of the blank are deleted. If no changes are required, the old string is used
valueof () overridden by: object,char[],char[] and Offset and count, Boolean,char,int,long,float,double returns a string that contains a character representation of the argument
Intern () None of each unique character order produces one (and only one) string handle

As you can see, each string method returns a new string object carefully once it is necessary to change the original content. Another issue to note is that if the content does not need to be changed, the method returns only one handle to the string. This can save storage space and system overhead.
The following is a list of methods for StringBuffer (string buffering) classes:

Method independent variable, covering use

Builder overridden: Default, the length of the buffer to be created, to create a new StringBuffer object based on the string it creates
ToString () does not create a string based on this StringBuffer
Length () No number of characters in StringBuffer
Capacity () does not return the current allocated space size
Ensurecapacity () is used to indicate the desired size of an integer to enable StringBuffer to accommodate at least the desired amount of space
SetLength () An integer that indicates the new length of a string within a buffer shortens or expands the previous one. If it is an extension, the void is filled with a null value
CharAt () An integer representing the location of the target element returns the char at the specified position of the buffer
Setcharat () An integer representing the position of the target element and a new char value of the element modifies the value at the specified position
GetChars () The starting and ending points of replication, the array to copy in and an index of the destination array to copy char to an external array. Unlike string, there is no GetBytes () available
Append () overridden by: object,string,char[], a specific offset and length char[],boolean,char,int,long,float,double converts the argument to a string and appends it to the end of the current buffer. If necessary, increase the length of the buffer at the same time
Insert () is overridden, and the first argument represents the position at which the insertion begins: Object,string,char[],boolean,char,int,long,float,double The second argument is converted to a string and inserted into the current buffer. The insertion position is at the beginning of the offset area. If necessary, increase the length of the buffer
Reverse () character order in no reverse buffer

One of the most common methods is append (). This method is used by the compiler when evaluating a string expression that contains the + and + = operators. The Insert () method takes a similar form. Both of these methods can perform important operations on the buffer and do not need to build a new object.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.