1. Common string construction methods are used:
1 Packagecn.itcast_01;2 3 /*4 * String: is a series of data consisting of multiple characters. It can also be seen as a character array. 5 * By looking at the API, we can know6 * A: string literal "abc" can also be seen as a string object. 7 * B: The string is a constant, and once assigned, it cannot be changed. 8 * 9 * Construction Method:Ten * Public String (): Empty construct One * public string (byte[] bytes): Convert byte array to string A * public string (byte[] bytes,int index,int length): Turns part of a byte array into a 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 the * public string (string original): Convert string constant value to string - * - * Method of String: - * public int length (): Returns the length of this string. + */ - Public classStringdemo { + Public Static voidMain (string[] args) { A //Public String (): Empty construct atString S1 =NewString (); -System.out.println ("S1:" +s1); -System.out.println ("S1.length ():" +s1.length ()); -System.out.println ("--------------------------"); - - //Public string (byte[] bytes): Convert byte array to string in byte[] Bys = {97, 98, 99, 100, 101 }; // -128 ~ 127 -String s2 =NewString (bys); toSystem.out.println ("S2:" +S2); +System.out.println ("S2.length ():" +s2.length ()); -System.out.println ("--------------------------"); the * //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"Panax NotoginsengString s3 =NewString (bys, 1, 3); -System.out.println ("S3:" +S3); theSystem.out.println ("S3.length ():" +s3.length ()); +System.out.println ("--------------------------"); A the //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 ("--------------------------"); - the //Public string (char[] Value,int index,int count): Turns a part of a character array into a string -String S5 =NewString (CHS, 2, 4);WuyiSystem.out.println ("S5:" +S5); theSystem.out.println ("S5.length ():" +s5.length ()); -System.out.println ("--------------------------"); Wu - //public string (string original): Convert string constant value to string AboutString s6 =NewString ("ABCDE"); $System.out.println ("S6:" +S6); -System.out.println ("S6.length ():" +s6.length ()); -System.out.println ("--------------------------"); - A //the string literal "abc" can also be seen as a string object. +String s7 = "ABCDE"; theSystem.out.println ("S7:" +S7); -System.out.println ("S7.length ():" +s7.length ()); $ } the}
2. The results of the operation are as follows:
Java Basics Hardening method for constructing string class of 30:string class