03-java String String Explanation

Source: Internet
Author: User

1. Java string

A, instantiate string string: Direct Assignment (more reasonable, use more), use the keyword new.

B. Comparison of string contents

// TODO auto-generated Method Stub //      int a=10; //      int b=10; //      System.out.println (a==b);       String str= "Hello";       String str1=new string ("Hello");       System.out.println (str==str1);  // The "= =" Comparison is the address       System.out.println (Str.equals (str1));  // "equals" compares the content, using only this

2. Common methods of String

(1) String length: Length ()

String str= "Jikexueyuan";

System. out. println (Str.length ()); The result is: 11

(2) string conversion array: ToCharArray ()

String str= "Jikexueyuan"; Char data[]=str.tochararray ();  for (int i=0;i<data.length;i++) {           System.out.print (Data[i]+ "  "); Result: J I k e x u e y u a n

(3) Remove the character from the string at the specified position: charAt ()

System.out.println (Str.charat (7)); Result: Y

(4) Conversion of a string to a byte array: getBytes ()

String str= "Jikexueyuan"; byte bytes[]=str.getbytes ();  for (int i=0;i<bytes.length;i++) {System.out.println (new String (bytes) + "\ T")

Results: Jikexueyuan

Jikexueyuan

Jikexueyuan

Jikexueyuan

Jikexueyuan

(5) Filter characters that exist in the string: IndexOf ()

String str= "[email protected]";

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

Results: 11

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

  String str= "      [email protected]";       System.out.println (str);      System.out.println (Str.trim ());

Results:

[Email protected]

[Email protected]

String str= "      [email protected]      ";       System.out.println (str+ "AA");       System.out.println (Str.trim ()+ "AA");

Results:

[Email protected] Aa

[Email protected]

(7) Remove substring from string: subString ()

(8) Case conversion: toLowerCase () toUpperCase () (Java is case sensitive)

(9) Determining the beginning and ending character of a string: EndsWith () Startwith ()

(10) Replacing one character in a string string: replace ()

3. Java StringBuffer method

StringBuffer, the buffer itself is an action string, but unlike string, StringBuffer can be changed. StringBuffer is an action class, so it must be manipulated by instantiation.

StringBuffer Common methods:

(1) Append (): Append

(2) Insert (): Insert string

StringBuffer sbf=New stringbuffer ();

Sbf.append ("Hello");

Sbf.insert (0, "I Love");//0 is the insertion position, appended to the front.

System. out. println (SBF. toString ());

Results: I Lovehello

(3) Replace (): Replace string

StringBuffer sbf=New  stringbuffer ();       Sbf.append ("Hello");       Sbf.insert (0, "I Love");       System.out.println (Sbf.tostring ());       Sbf.replace (1, 3, "Wwwtliu"); // Replace from 2nd position to 4th position with Wwwtliu       System.out.println (Sbf.tostring ());

Results:

I Lovehello

Iwwwtliuovehello

(4) IndexOf (): Similar to string

Applications of the StringBuffer class:

StringBuffer str =new  stringbuffer (); Str.append ("Jikexueyuan");  for (int i = 0; i < i++) {str.append (i);} System.out.println (Str.tostring ());

4. Java Stringbuider Usage

(1) A mutable sequence of characters that is designed to be a simple replacement of the stringbuffer, used when a string buffer is used by a single thread. It is recommended to give priority to this class, faster than StringBuffer.

(2) However, if you design to thread safety, we recommend that you use StringBuffer

(3) Common methods: Append () and insert ()

03-java String String Explanation

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.