12-02 Java String class

Source: Internet
Author: User
Tags array to string

String construction method

 Packagecn.itcast_01;/** String: is a series of data consisting of multiple characters. It can also be seen as a character array. * By looking at the API, we can know that * A: the string literal "abc" can also be seen as a string object. * B: The string is a constant, and once assigned, it cannot be changed.  * * Construction Method: * Public string (): Empty construct * public string (byte[] bytes): Convert byte array to String * public string (byte[] Bytes,int index,int Length): Convert part of byte array to String * public string (char[] value): Convert character array to String * public string (char[]         Value,int index,int count): Turns a part of a character array into a string * public string (string original): The method of turning a string constant value into a string * * string: * public int Length (): Returns the length of this string. */ Public classStringdemo { Public Static voidMain (string[] args) {//Public String (): Empty constructString S1 =NewString (); System.out.println ("S1:" +S1); System.out.println ("S1.length ():" +s1.length ()); System.out.println ("--------------------------"); //S1://s1.length (): 0//Public string (byte[] bytes): Convert byte array to string        byte[] Bys = {97, 98, 99, 100, 101 }; String S2=NewString (bys); System.out.println ("S2:" + s2);//ABCDESystem.out.println ("S2.length ():" + s2.length ());//System.out.println ("--------------------------"); //Public string (byte[] bytes,int index,int length): Turns a portion of a byte array into a string//I want to get the string "BCD"String s3 =NewString (bys, 1, 3); System.out.println ("S3:" +S3); System.out.println ("S3.length ():" +s3.length ()); System.out.println ("--------------------------"); //Public string (char[] value): Convert character array to string        Char[] CHS = {' A ', ' B ', ' C ', ' d ', ' e ', ' Love ', ' Forest ', ' pro ' }; String S4=NewString (CHS); System.out.println ("S4:" +S4); System.out.println ("S4.length ():" +s4.length ()); System.out.println ("--------------------------"); //Public string (char[] Value,int index,int count): Turns a part of a character array into a stringString S5 =NewString (CHS, 2, 4); System.out.println ("S5:" +S5); System.out.println ("S5.length ():" +s5.length ()); System.out.println ("--------------------------"); //public string (string original): Convert string constant value to stringString s6 =NewString ("ABCDE"); System.out.println ("S6:" +S6); System.out.println ("S6.length ():" +s6.length ()); System.out.println ("--------------------------"); the//string literal "abc" can also be seen as a string object. String s7 = "ABCDE"; System.out.println ("S7:" +S7); System.out.println ("S7.length ():" +s7.length ()); }}

Character of the string: once assigned, it cannot be changed.

But references can change

 Package cn.itcast_02; /*  */Publicclass  stringdemo    {publicstatic void Main (string[] args) {        = "Hello";         + = "World";        SYSTEM.OUT.PRINTLN (//  HelloWorld    }}

Graphic:

string s = new string ("Hello") and string s = "Hello";

The difference between a string literal object and a constructor method for creating an object

 Packagecn.itcast_02;/** String s = new string ("Hello") and string s = "Hello"; * there is. The former creates 2 objects, which create 1 objects. * = =: Comparing the reference type is the same as the address value * equals: Comparing the reference type by default is also the same as comparing the address value, and the string class overrides the Equals () method, comparing whether the content is the same. */ Public classStringDemo2 { Public Static voidMain (string[] args) {String S1=NewString ("Hello"); String S2= "Hello"; System.out.println (S1==S2);//falseSystem.out.println (S1.equals(S2));//True    }}

Graphic:

String s5 = "Hello";         = "Hello";         = = S6); //string literal, directly from memory, so true        System.out.println (S5.equals (S6)); // true

 Packagecn.itcast_02;/** See the program write results * String If the variables are added, first open space, in stitching. * String If the constant is added, is added first, then in the constant pool to find, if there is a direct return, otherwise, created. */ Public classStringDemo4 { Public Static voidMain (string[] args) {String S1= "Hello"; String S2= "World"; String S3 = "HelloWorld"; System.out.println (S3        = = S1 + s2);//False. String if the variable is added, first open space, and then splicing. System.out.println (s3.equals (s1 + s2));//trueSystem.out.println (s3 = = "Hello" + "World");//true. If the string is a constant addition, it is added first, then the constant pool is searched, if there is a direct return, otherwise, it is created. System.out.println (s3.equals ("Hello" + "world");//true//through the anti-compilation to see the source code, we know this has been done to deal with. //System.out.println (s3 = = "HelloWorld"); //System.out.println (s3.equals ("HelloWorld"));    }}

The judgment function of the string class:

 Packagecn.itcast_03;/** The function of the string class: * boolean equals (Object obj): Compares the contents of a string to the same, case-sensitive * Boolean equalsignorecase (String str): Compares the contents of a string, ignoring case * Boolean contains (String str): Determines whether a large string contains a small string * Boolean StartsWith (String str): Determines whether a string begins with a specified string * Boolean endsWith (String str): Determines whether a string ends with a specified string * Boolean IsEmpty (): Determines whether the string is empty. * * Note: * string contents are empty and string objects are empty. * String s = "";//object exists, so can be adjusted method * String s = null;//object does not exist, cannot tune method */ Public classStringdemo { Public Static voidMain (string[] args) {//creating a String ObjectString S1 = "HelloWorld"; String S2= "HelloWorld"; String S3= "HelloWorld"; //boolean equals (Object obj): Compares the contents of strings for the same, case-sensitiveSystem.out.println ("Equals:" +s1.equals (S2)); System.out.println ("Equals:" +S1.equals (S3)); System.out.println ("-----------------------"); //boolean equalsignorecase (String str): Compares whether the contents of a string are the same, ignoring caseSystem.out.println ("Equals:" +s1.equalsignorecase (S2)); System.out.println ("Equals:" +S1.equalsignorecase (S3)); System.out.println ("-----------------------"); //Boolean contains (String str): Determines whether a large string contains a small stringSystem.out.println ("contains:" + s1.contains ("Hello")); System.out.println ("Contains:" + s1.contains ("HW")); System.out.println ("-----------------------"); //boolean StartsWith (String str): Determines whether a string begins with a specified stringSystem.out.println ("StartsWith:" + s1.startswith ("H")); System.out.println ("StartsWith:" + s1.startswith ("Hello")); System.out.println ("StartsWith:" + S1.startswith ("World")); System.out.println ("-----------------------"); //Exercise: Boolean endsWith (String str): Determines whether a string ends with a specified string.//boolean isEmpty (): Determines whether the string is empty. System.out.println ("IsEmpty:" +s1.isempty ()); String S4= ""; String S5=NULL; System.out.println ("IsEmpty:" +s4.isempty ()); //NullPointerException//S5 object does not exist, so method cannot be called, NULL pointer exception//System.out.println ("IsEmpty:" + s5.isempty ());            }}

12-02 Java String class

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.