JAVA learning notes-string overview, java learning notes Overview

Source: Internet
Author: User

JAVA learning notes-string overview, java learning notes Overview

I. String class

A String class represents a String, a sequence of characters. The method for creating a String object is simple. There are the following:

1) create with new:

  String s1 = 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'};  String s3 = new String(c);

String objects are unchangeable and their values cannot be changed after creation.Every method in the String class that looks to modify the String value is actually creating a brand new String object to include the modified String content.. As shown below:

String s = "tongye"; // creates a String object with the content of "tongye" and assigns its reference to s, s is a String type reference s = "TONGYE"; // creates a new String object whose content is "TONGYE" and assigns its reference to s.

After the second statement is created, the first String object "tongye" does not disappear, but it cannot be accessed, because the reference s points to another object "TONGYE ".

The String class has 15 constructor methods and many methods for processing strings. The following is a brief introduction:

String (); // initialize a newly created String object to represent an empty character String (byte [] bytes ); // use the default Character Set of the platform to decode the specified byte array and construct a new StringString (byte [] bytes, Charset charset); // use the specified Charset to decode the specified byte array, construct a new StringString (byte [] bytes, int offset, int length); // use the default character Street on the platform to decode the specified byte sub-array and construct a new String, here, offset is the index of the first character, and length is the length of the sub-array String (StringBuilder builder); // assign a new String, it contains the Character Sequence String (StringBuffer buffer) currently contained in the String generator parameter; // It is allocated with a new String, which contains the Character Sequence currently included in the String buffer Parameter

 

CharAt (int index); // returns the char value s1.compareTo (s2) at the specified index; // compares the two strings s1 and s2 in alphabetical order, returns an int type data s1.equals (s2); // compares s1 WITH s2. If it is completely equal, true s1.contact (s2) is returned ); // connect s2 to the end of string s1 toString (); // return the toUpperCase () of the object itself; // convert all characters into uppercase valueOf (boolean B ); // returns the string representation of the boolean parameter substring (int beginIndex, int endIndex); // returns a new string, which is a substring of the string, split (regex) between beginIndex and endIndex; // use the given regular expression to split this string

 

Ii. StringBuffer and StringBuilder classes

Once a String object is created, its content cannot be changed, which makes the String class not very flexible and inconvenient in some cases. The StringBuffer and StringBuilder classes are another solution that can replace the String class to process strings. They are more flexible than the String class: you can add, insert, or append new content to a StringBuffer or StringBuilder class. Generally, the StringBuffer or StringBuilder class can be used wherever strings are used.

1. StringBuffer

StringBuffer is a thread-safe variable character sequence, a String-like buffer, but cannot be modified. StringBuffer has multiple string processing methods, including append and insert. StringBuilder is thread-safe.

Append (boolean B); // append the string representation of a boolean parameter to the end of the sequence. Other parameter types are similar to insert (int offset, char c ); // Insert the string representation of the char type parameter to the end of the character with the index of this sequence as offset: delete (int start, int end ); // Delete the deleteCharAt (int index) character between the index number start and end; // remove the char at the specified position in the sequence

2. StringBuilder

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

 

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.