Lang packet Knowledge Point (ii) String, StringBuffer

Source: Internet
Author: User

String class:

1. In Java, strings are objects of the string class, and 2 can be done by using the methods provided by the String class; 3. After creating a string object, the You cannot change the characters that make up a string ; 4, whenever a string version is changed, a new String object is created, and contains the changes made in it, The original string remains the samecommon construction methods for the String class:
String ()    // will create an empty string (string original)  / /creates a new string as a copy of the specified string  string (char[] value) // will construct a new string based on a character array string (byte [] tytes) // will create a new string by converting the specified byte array        

String length:

String str1 = "John Smith"; System.out.println (str1.length ());

string comparison:

1 . To determine whether two strings are equal, you can use the "= =" operator and the Equals () method, but the results may not be exactly the same

2. the operator "= =" is used to compare whether two references point to the same object, The Equals () method compares whether the contents of the two strings are the same. string Judgments and search methods:    
//determines whether the string anotherstring is equal to the current string, ignoring the case formBooleanequalsignorecase (String anotherstring)//compares the size of the string anoterstring and the current string.intcompareTo (String anotherstring)//determines whether the current string starts with a string prefixBooleanstartsWith (String prefix)//determines whether the current string is suffixed with a string suffixBooleanendsWith (String suffix)//=========================================================================================//The search character ch is the first occurrence of the index in the current string and returns 1 if it does not appearintIndexOf (intch)//search string str The first occurrence of the index in the current string, no return 1intindexOf (String str)//The search character ch is the last occurrence of the index in the current string and returns 1 if it does not appearintLastIndexOf (intch)//search string str The last occurrence of the index in the current string, which does not appear returns-1intLastIndexOf (String str)

methods for intercepting and replacing strings:     
//used to extract a single character from a specified location, specified by index, and the index value must be a non-negativeCharCharAt (intindex)//used to extract the part of a string starting at the position specified by indexString substring (intindex)//used to extract the string portion between the begin and end positionsString substring (intBeginintend)//used to concatenate two strings and create a new string object that contains the calling stringstring concat (String str)
//=============================================================================
//used to replace all occurrences of the Oldchar specified character in the invocation string with the specified character NewcharString Replace (CharOldChar,CharNewchar)//used to replace all strings that appear in the invocation string or match the regex with the characters specified by replacementReplaceAll (string regex, string replacement)//used to return a copy of a call string that does not contain any spaces before or after itString Trim ()
change the case of a string
String toUpperCase ()  // returns the current string in all uppercase form string toLowerCase ()  //  Returns the full lowercase form of the current string

string format conversion:

// returns the form of the current string converted to a byte array (that is, the most primitive binary form that the string is stored in memory) byte [] getBytes () // returns the character array form of the current string Char [] ToCharArray ()

StringBuffer class:      1. The StringBuffer class is used to represent a string that can be modified; 2, a string using the + operator will automatically create a string buffer object common methods of the StringBuffer class:           the difference between string and StringBuffer:     

1 . This string class provides a string of values that cannot be changed. The string provided by this StringBuffer class is modified. You can use StringBuffer when you know that character data is going to change.

Typically, you can use Stringbuffers to dynamically construct character data.

2. In addition,string implements the Equals method , new string ("ABC"). The result of equals (NewString ("abc") is True, and StringBuffer does not implement the Equals method , so, new

StringBuffer ("abc"). Equals (Newstringbuffer ("abc") results in false.

Lang packet Knowledge Point (ii) String, StringBuffer

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.