Packagecom.string;Importjava.io.UnsupportedEncodingException;ImportJava.nio.charset.Charset; Public classStringtest { Public Static voidMain (string[] args) {/*** String (byte[] bytes) * Constructs a new string by decoding the specified byte array using the platform's default character set. */ byte[] B1 = {' A ', ' B ', ' C ', ' d '}; System.out.println (NewString (B1));//ABCD /*** String (byte[] bytes, Charset Charset) * Constructs a new string by decoding the specified byte array using the specified Charset. */System.out.println (NewString (B1, Charset.forname ("UTF-8")));//CharSet represents the encoding method /*** String (byte[] bytes, int offset, int length) * Constructs a new String by decoding the specified byte Subarray by using the platform's default character set. */System.out.println (NewString (B1, 1, 2));//BC starts with the 2nd character and then takes two characters to stitch into a string /*** String (byte[] bytes, int offset, int length, Charset Charset) * Constructs an array of bytes specified by using the specified Charset decoding A new String. */System.out.println (NewString (B1, 1, 3, Charset.forname ("UTF-8")));//BCD /*** String (byte[] bytes, int offset, int length, String charsetname) * Constructs a new one by decoding the specified byte subarray using the specified character set. String. */ Try{System.out.println (NewString (B1, 0, 2, "UTF-8"));//AB}Catch(unsupportedencodingexception e) {e.printstacktrace (); } /*** String (byte[] bytes, string charsetname) * Constructs a new string by decoding the specified byte array using the specified charset. */ Try{System.out.println (NewString (B1, "UTF-8"));//ABCD}Catch(unsupportedencodingexception e) {e.printstacktrace (); } /*** String (char[] value) * Allocates a new String that represents the sequence of characters currently contained in the character array parameter. */System.out.println (NewString (B1));//ABCD /*** String (char[] value, int offset, int count) * Assigns a new String that contains characters from a subarray of character array parameters. */System.out.println (NewString (B1, 2, 2));//CD /*** String (int[] codepoints, int offset, int count) * Assigns a new String that contains the Unicode code points Group parameter to the character of a sub-array. */ int[] B2 = {65,66,67,68,97,97}; System.out.println (NewString (B2, 1, 3));//BCD converts Unicode code to corresponding character /*** String (string original) * Initializes a newly created string object that represents a sequence of characters identical to the parameter; in other words, the newly created string is a copy of the parameter string. */System.out.println (NewString ("ABCD"));//ABCD and New String () differ in memory allocation /*** String (stringbuffer buffer) * Assigns a new string containing the sequence of characters currently contained in the string buffer parameter*/StringBuffer B3=NewStringBuffer ("ABCD"); System.out.println (NewString (b3));//ABCD /*** String (StringBuilder builder) * Assigns a new string that contains the sequence of characters currently contained in the string generator parameter. */StringBuilder B4=NewStringBuilder ("ABCD"); System.out.println (NewString (b4));//ABCD }}
Java String Constructor