A String object in Java

Source: Internet
Author: User

Characteristics of a String object
    1. The data of the string object cannot be changed!

    2. String type object encapsulates an array of strings

    3. No action can change the contents of this character array

      string s =  "123"; string ss = s;s = s+ "ABC"; System.out.println (s);//123abcsystem.out.println (ss);//123 

Description: A string reference variable was changed in the above code, but the string did not change! The advantage is that strings can be used "as" Basic types!

Principle:

The reuse of string constants

java are reusable in every possible use! The benefit is saving resources (memory)

    1. String literal (direct amount) reuses the same string object at the same time as the content. String S1 = "123ABC"; String s2 = "123ABC"; S1 S2 is a reference variable of string type//"123ABC" is a direct volume (literal)

    2. String constant is also involved in reuse!

    3. Literal, constant operation result is a string, also reuse the same string s4 = "123ABC"; String S5 = 123 + "abc",//1+ "23abc" System.out.println (S4==S5),//true

    4. String variable, the result of the operation of the variable and the newly created string object are not involved in reuse!! String name = In.nextline ();//tom string S1 = "Tom and Jerry"; String s2 = name + "and Jerry"; System.out.println (S1==S2);//false string s3 = new String ("Tom and Jerry"); System.out.println (S3==S1);//false

Principle:

Classic topics:

String S1 = "1" + "+" + "abc"; String s2 = "1" +23+ "abc"; String s3 = ' 1 ' +23+ "abc"; System.out.print (S1==S2); System.out.print (S1==S3); execution result of the above code: A.truetrue b.truefalse c.falsetrue D.falsefalse
Characters in a string

A character array is encapsulated in the string, and the characters in the string are data of type char.

    1. The char type is an integer and is a Unicode encoding of a character.

    2. 16-bit unsigned integer that occupies 2 bytes

Case:

String s = "Tom and Jerry";//0123456789012char C = S.charat (4); System.out.println (c);//asystem.out.println ((int.) c);//97
IndexOf method

Find the position of a character in a string:

IndexOf () 1. If there are duplicates, find the first position on the left 2. If not found, return-1

Case:

String s = "Tom and Jerry"; int i = S.indexof (' a '); System.out.println (i);//4i = S.indexof (' R '); System.out.println (i);//10i = S.indexof (' X '); System.out.println (i);//-1

Str.indexof ("Find string", starting position)

String url = "http://tedu.cn/index.html"; int i = Url.indexof ("/", 7); System.out.println (i);
LastIndexOf

Reverse-Order Lookup: Right-to-left lookup, returning the position of a character

String url = "http://tedu.cn/index.html"; int i = Url.lastindexof ("/");//14system.out.println (i);//14
    1. Find Package Java.lang

    2. Found class String

    3. Find Method LastIndexOf ()

Substring method

truncate a portion of the string as a substring

Url.substring (start position)//from start position to last intercept as substring string url = "Http://tedu.cn/index.html"; string filename = url.substring;//filename = index.htmlurl.substring (start position, end position)//from start position to end position intercept as substring string url = " Http://tedu.cn/index.html ";//01234567890123456//includes the starting not including the end position of string str = url.substring (7, 14); String str = url.substring (7, 7+8);
Trim
String str = "\ t Tom \ r \ n"; String s = Str.trim ();
StartsWith EndsWith

Detects whether a string begins or ends with a specified string

String str = "Hello world!"; Boolean B = Str.startswith ("Hello"),//trueb = Str.startswith ("World"),//falseb = Str.endswith ("World");//falseb = Str.endswith ("!"); /true

Case:

String name = "Demo." JPG ";" if (Name.tolowercase (). EndsWith (". jpg")) {System.out.println ("picture file");}
StringBuilder

Java provides an API for calculating strings, which performs well:

Case:

String s = "A"; s = s + "1"; s = s + "1"; s = s + "1"; System.out.println (s);

Principle:

performance comparison:

    String s =  "A";    long t1 =  System.currenttimemillis ();     for (int i=0; i<10000; i++) {         s = s+ "1";     }    long  t2 = system.currenttimemillis ();     system.out.println (S.length ());     system.out.println (T2-T1);    stringbuilder ss =          new stringbuilder ("A");     t1= System.nanotime ();     for (int i=0; i<10000; i++) {         ss.append ("1");    }    t2 =  System.nanotime ();     system.out.println (Ss.length ());     System.out.println (T2-T1);


A String object 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.