Some core content of strings in Java

Source: Internet
Author: User

first, thecommon method of StringBuilder class in Java

Append (parameter): Append content to the end of the current StringBuilder object

Insert (position, parameter): Inserts the content into the specified position of the StringBuilder object

ToString (): Converts a StringBuilder object to a String object

Length (): Gets the length of the string

toLowerCase (): Convert to lowercase

charAt (int i): Gets the value indexed to I

GetBytes (): Convert to byte array

indexOf (int ch): Find the ch character where the string first appears

IndexOf (String str): Find the first occurrence of the STR string in the position of the string

Lastindexof (int ch): Find the CH character at the last occurrence of the string

LastIndexOf (String str): Finds the position of the STR string at the last occurrence of the string

Split (): Splits the string into substrings;

1 "Append and insert operations for StringBuilder objects

1 StringBuilder str=new StringBuilder ("Hello"); 2 str.append ("World"); 3 str.append ("!") ); 4 System.out.println (Str.length ()); 5 System.out.println (str);
6 Str.insert (12. "!");
7 Sting str2=str.tostring ();
8 System.out.println ("After insertion:" +STR2);


Output Result: 12

Hello world!

After insertion: Hello world!!

Note : StringBuffer and StringBuilder function the same, but StringBuffer is thread-safe, and StringBuilder does not implement thread-safe functionality, so performance is slightly higher. Therefore, in general, if you need to create a variable-content string object, you should consider using the StringBuilder class as a priority.

Second,"= =" and equals () What is the difference?

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

Equals (): Compares the content stored in two string objects in a consistent

1 String str1= "I love You"; 2 string str2=new string ("I Love You"); 3 System.out.println ("str1 and str2 have the same memory address?") "+ (str1==str2)); 4 System.out.println ("str1 and str2 have the same content? "+str1.equals (STR2));

Output Result: false

True

Some core content of strings in Java

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.