The Java string string/== differs from Equals, str. Tocharat (), Getbytes,indexof filter exists character, trim ()/string with StringBuffer multithreading security/stringbuilder single thread--14.0

Source: Internet
Author: User

Course Outline

String strings

String String Common methods

StringBuffer

StringBuilder

String strings:

1. Instantiating a String object

Direct assignment String str= "Hello"; Recommend this

Use the keyword new string str1=new string ("Hello"); Open 2 spaces in memory

Source Code Stringdemo01.java

Comparison of 2.String content

String str= "Hello";
String Str1=new string ("Hello");
System.out.println (STR==STR1); False
System.out.println (Str.equals (str1));
the "= =" Comparison is the memory address equals comparison is the content

Detailed source code is as follows Stringdemo02.java

3. String contents cannot be changed

String str= "Hello";
String str1=str+ "World";
System.out.println (STR1);

The feeling is a combination of changes but through the diagram

Stringdemo03.java

#String字符串常用方法

1. Length of string: Long () method

2. String conversion array: ToCharArray ();

3. Remove the character charat () from the string in the specified position;

4. Conversion of strings to byte arrays: getBytes ()

5. Filter the characters that exist in the string: IndexOf () has returned the current position subscript no return-1

System.out.println (Str.indexof ("@"));

6. Remove the front and back spaces of the string trim ()

System.out.println (Str.trim ());

7. Extracting substrings from a string
8. Case conversion toLowerCase () toUpperCase ();
9. Determine the beginning and ending character of a string, Endwith () Startwith ()
10. Replace one character in a string string replace ()

Stringdemo04.java

#StringBuffer

1. Understanding StringBuffer
Buffer, which is itself an action string, but unlike string, StringBuffer can be changed,
That is, a string is a constant stringbuffer is a variable stringbuffer is an action class, so it must be manipulated by instantiation

StringBuffer sb=new StringBuffer ();
Sb.append ("Jikexueyuan")//append add content SQL statement before and after space
System.out.println (Sb.tostring ());

Stringbufferdemo01.java

2.StringBuffer Common methods
Append ()//Append
Insert () Inserts this method
Replace ()
IndexOf

Stringbufferdemo02.java

Application of 3.StringBuffer

String str= "Jikexueyuan";
for (int i = 0; i <; i++) {
Str=str+i;

}
System.out.println (str);
Open 100 Spaces +STR1 = 101

}

4.0 StringBuilder

1.  Comparison of speed of execution: StringBuilder >  stringbuffer   
2. StringBuffer and StringBuilder, they are string variables, are objects that can be changed, and whenever we use them to manipulate strings, we actually operate on an object and do not create objects like string to manipulate them, so the speed is fast.
3. StringBuilder: Thread non-secure
StringBuffer: Thread-safe
When we are using a string buffer to be used by multiple threads, the JVM does not guarantee that the StringBuilder operation is safe, although he is the fastest, but can ensure that the StringBuffer can be operated correctly. Of course, most of the time we are in a single-threaded operation, so in most cases it is recommended to use StringBuilder instead of StringBuffer, which is the reason for speed.

Summary of the use of the three: 1. If you want to manipulate a small amount of data with = String
       2. Manipulating large amounts of data under a single-threaded operation string buffer = StringBuilder
       3. Multi-threaded operation string buffer operation large amount of data = StringBuffer

The Java string string/== differs from Equals, str. Tocharat (), Getbytes,indexof filter exists character, trim ()/string with StringBuffer multithreading security/stringbuilder single thread--14.0

Related Article

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.