About String and StringBuffer

Source: Internet
Author: User

Some features of the String class

1: It is a special class. Once initialized, the content cannot be changed. Generally, there are two initialization methods.

-- | String s1 = "abc"; // abc exists in the String constant pool, which is an object. If you need to use abc in future code, you can directly use it, you do not need to open up the space of the string abc

-- | String s2 = new String ("abc"); // with the new Keyword, you need to open up an abc space in the heap memory, stack s2 stores the abc address in the stack.

What is the difference between s1 and s2? S1 has only one object in the memory, and s2 has two objects in the memory.

Public class StringDemo {public static void main (String [] args) {/** String feature * once a String object is initialized, it will not be changed */String s1 = "abc "; // "abc" exists in the String constant pool. If it is not created, it is directly created using String s2 = "abc"; System. out. println (s1 = s2); // true String s3 = "nba"; String s4 = new String ("nba "); // create an nba Space System in the heap memory. out. println (s3 = s4); // false, compare the address, one in the constant pool, and the other in the heap memory System. out. println (s3.equals (s4); // In String, equals (), returns equals () in OBJ, which compares content }}

Some common methods in the String class

1: Get

Int length ();

Char charAt (int index );

Int indexOf (String str); the first position of the index str. If str does not exist,-1 is returned. It can be used to determine whether the specified String exists.

It will be reflected in the next few small exercises

Int indexOf (String str, int fromIndex );

Int lastIndexOf (String str );

2: Judgment

Boolean contains (String str );

Boolean isEmpty ();

boolean endsWith(String str);

boolean startsWith(String str);

Boolean equals (String str); determines whether the content of a String is the same, and whether the content of a String is case sensitive.

Boolean inclusignorecase (); determines whether the content of the string is the same. This is case-insensitive.

3: Conversion

3.1 convert the character array into a string.
Constructor: String (char [])
String (char [], offset, count): convert a part of the character array into a String.

Static Method:
Static String copyValueOf (char []);
Static String copyValueOf (char [] data, int offset, int count );

Static String valueOf (char []):

4: replace

String replace (String oldChar, String newChar );

5. Obtain the substring of a string.

String subString (int beginIndex); beginIndex is the starting position for intercepting the index of a subString, including this position.

String subString (int beginIndex, int endIndex); gets the subString between two index values, header, without the tail

6. Convert, remove spaces, and compare.
Converts a string to uppercase or lowercase.
String toUpperCase ();
String toLowerCase ();

Remove multiple spaces at both ends of the string.
String trim (); this method will play a major role in the design of user input passwords and user names in future development.

Compares two strings in natural order.
Int compareTo (string );

Bytes --------------------------------------------------------------------------------------------------------------------

StringBuffer

StingBuffer is a container and a string buffer. It has the following features: the length can be changed, multiple data types can be operated in bytes, and finally changed to a string through toSting ().

1. Storage.
StringBuffer append (): add the specified data as a parameter to the end of the existing data.
StringBuffer insert (index, data): You can insert data to the specified index position.


2. Delete.
StringBuffer delete (start, end): delete data in the buffer, including start, not end.
StringBuffer deleteCharAt (index): delete a character at a specified position.

3. obtain.
Char charAt (int index)
Int indexOf (String str)
Int lastIndexOf (String str)
Int length ()
String substring (int start, int end)

4. modify.
StringBuffer replace (start, end, string );
Void setCharAt (int index, char ch );


5. Reverse.
StringBuffer reverse ();
6. Store the specified data in the buffer to the specified character array.
Void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin)

StringBuffer is used frequently during development. Why?

When using String, when we change its value, the memory address of String will also change. For StringBuffer, change its value, the memory address it occupies will not change

From this point of view, StringBuffer is more efficient than String

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.