Summary of Java string simple knowledge points

Source: Internet
Author: User

1. Comparison of strings

1  Public voidrun () {2         //str1 in the pool3String str1 =NewString ("string");4         //STR2,STR3 exist in the heap5String str2 = "string";6String STR3 = "Strin" + "G";7         8System.out.println (str1 = = str2);//false9         TenSystem.out.println (Str1.equals (str2));//true One          ASystem.out.println (Str1.equalsignorecase (STR3));//true -          -System.out.println (Str1.tolowercase (). Equals (STR3));//true the          -}
== "and equals () What is the difference?

Equals (): Checks whether the characters that make up the string contents are exactly the same

= =: Determine the first address of a two string in memory, that is, whether it is the same string object

2. Connection of strings

1  Public voidrun () {2         //str1 in the pool3String str1 =NewString ("string");4         //str2 exist in the heap5String str2 = "string";6         //Method 1:7String STR3 = str2+ "FFF";8         //Method 2:9String STR4 = Str2.concat ("FFFF");Ten          One}

3. Extraction of strings

Method

Description

public int indexOf (int ch)

Search for the first occurrence of the character ch (or string value)

public int indexOf (String value)

public int lastIndexOf (int ch)

Search for the last occurrence of the character ch (or string value)

public int lastIndexOf (String value)

Returns the position where the first match occurred , or 1 if no character or string was found;

Method

Description

Public String substring (int index)

Extracts the string portion starting from the position index

Public String substring (int beginindex, int endindex)

Extracting the string part between Beginindex and Endindex

Public String trim ()

Returns a copy of a call string that contains no spaces before or after it

Beginindex: The position of the string is calculated starting from 0, Endindex: The position of the string is calculated from 1;

4. Splitting of strings

The String class provides the split () method, splits a string into substrings, and returns the result as a string array
1  Public voidrun () {2 3String words= "Changting outside the ancient road edge of grass green breeze Liudisheng sound remnant Sunset Mountain outside Mountain";4String[] Printword=Newstring[100]; 5SYSTEM.OUT.PRINTLN ("* * * Original lyrics format ***\n" +words);6System.out.println ("\n*** lyrics format after splitting");7Printword=words.split (""); 8          for(inti=0;i<printword.length;i++){9 System.out.println (Printword[i]);Ten         } One      A  -}

5. Creation of strings

• When Java executes, a string pool is maintained, and when the "" is used directly in the program to include a string, the string is in the string pool.

• For some string objects that can be shared, first look in the string pool for the existence of the same string content (the same character), and if so, return directly instead of creating a new string object directly to reduce memory consumption.

When the Intern () method is called, if the same string object is already included in the pool (the same or not as determined by the Equals () method), the string is returned from the pool, otherwise the original string object is joined to the pool and returns a reference to the string object.

Summary of Java string simple knowledge points

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.