Java Learning Notes--string overview

Source: Internet
Author: User

First, the String class

String classes represent strings, which are a sequence of characters. The method of creating a String object is simple, in the following ways:

1) Use new to create:

New String ("My name is Tongye");

2) create directly without NEW:

String s2 = "My name is Tongye";

3) You can use a character array to create a string:

  Char [] c = {' t ', ' o ', ' n ', ' g ', ' y ', ' e 'new String (c);

String objects are immutable, and their values cannot be changed after they are created. every method in the string class that looks to modify a string value is actually creating a completely new string object that contains the modified string content . Like this:

String s = "Tongye"; // a string object with the content "Tongye" is created and its reference assigned to S,s is a string-type reference       = "Tongye";  //  creates a new string object with the content "Tongye" and assigns its reference to S  

After the second statement is created, the first string object "Tongye" does not disappear, but it can no longer be accessed because reference S is now pointing to another object "Tongye".

There are 15 ways to construct a string class, and many ways to handle strings, and here's a brief introduction:

String ();//Initializes a newly created string object that represents a sequence of empty charactersString (byte[] bytes);//uses the default character set of the platform to decode the specified byte array, constructing a new stringString (byte[] Bytes,charset Charset);//decodes the specified byte array using the specified charset, constructing a new stringString (byte[] Bytes,intOffsetintlength);//uses the default character street of the platform to decode the specified byte Subarray, constructing a new string, where offset is the index of the first character, and length is the size of the sub-arrayString (StringBuilder builder); //assigns a new string that contains the sequence of characters currently contained in the string generator parameterString (stringbuffer buffer); //assigns a new string that contains the sequence of characters currently contained in the string buffer parameter

CharAt (intindex);//returns the char value at the specified indexS1.compareto (S2); //compares two strings of S1 and S2 in dictionary order, returning an int type datas1.equals (S2); //compares S1 to S2 and returns true if they are exactly equals1.contact (S2); //Connect S2 to the end of the string S1toString (); //returns the object itselftoUpperCase (); //convert all characters to uppercaseValueOf (Booleanb);//returns the string representation of a Boolean parametersubstring (intBeginindex,intEndIndex);//returns a new string, which is a substring of the string, between Beginindex and Endindexsplit (regex); //splits this string using the given regular expression

Second, StringBuffer and StringBuilder categories

Once a string object is created, its contents cannot be changed, which makes the string class not very flexible and inconvenient in some situations. The StringBuffer and StringBuilder classes are another solution for handling strings that can replace the string class, which is more flexible than the string class: You can add to a StringBuffer or StringBuilder class, Insert or append new content. In general, you can use the StringBuffer or StringBuilder class wherever a string is used.

1, StringBuffer

StringBuffer is a thread-safe, variable-character sequence that resembles a string buffer, but cannot be modified. StringBuffer has several methods for handling strings, with the main operations being the append and insert methods. StringBuilder is thread-safe.

Append (boolean b);    // appends the string representation of a Boolean type argument to the end of the sequence, similar to the other parameter types Insert (int offset,char c);    // inserts the string representation of a char type parameter after the character for which the sequence index is offset Delete (int start,int end);    // Remove the character between the index number start and end Deletecharat (int index);    // removes the char at the specified position of the sequence

2, StringBuilder

StringBuilder is a variable sequence of characters. It provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed as a simple replacement for StringBuffer, when the string buffer is used by a single thread. In most cases, it is faster than StringBuffer. It is not safe to use an instance of StringBuilder for multiple threads.

Java Learning Notes--string overview

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.