Chapter: Java The Complete Reference - The Java Library - String Handling

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

The String Constructors

The String class supports several constructors. To create an empty String, call the default constructor. For example, String s = new String();

The String Constructors

 

The String class supports several constructors. To create an empty String, call the default constructor. For example,

 

String s = new String();

 

will create an instance of String with no characters in it.

 

Frequently, you will want to create strings that have initial values. The String class provides a variety of constructors to handle this. To create a String initialized by an array of characters, use the constructor shown here:

 

String(char chars[ ]) Here is an example:

 

char chars[] = { 'a', 'b', 'c' }; String s = new String(chars);

 

This constructor initializes s with the string "abc".

 

You can specify a subrange of a character array as an initializer using the following constructor:

 

String(char chars[ ], int startIndex, int numChars)

 

Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use. Here is an example:

 

char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' };

String s = new String(chars, 2, 3);

 

This initializes s with the characters cde.

 

You can construct a String object that contains the same character sequence as another String object using this constructor:

 

String(String strObj)

 

Here, strObj is a String object. Consider this example:

 

// Construct one String from another.

class MakeString {

 

public static void main(String args[]) {

char c[] = {'J', 'a', 'v', 'a'}; String s1 = new String(c);

 

String s2 = new String(s1);

 

System.out.println(s1);

 

System.out.println(s2);

 

}

 

}

 

The output from this program is as follows:

 

Java

 

Java

As you can see, s1 and s2 contain the same string.

 

Even though Java’s char type uses 16 bits to represent the basic Unicode character set, the typical format for strings on the Internet uses arrays of 8-bit bytes constructed from the ASCII character set. Because 8-bit ASCII strings are common, the String class provides constructors that initialize a string when given a byte array. Two forms are shown here:

 

String(byte chrs[ ])

 

String(byte chrs[ ], int startIndex, int numChars)

Here, chrs specifies the array of bytes. The second form allows you to specify a subrange. In each of these constructors, the byte-to-character conversion is done by using the default character encoding of the platform. The following program illustrates these constructors:

// Construct string from subset of char array.

class SubStringCons {

 

public static void main(String args[]) { byte ascii[] = {65, 66, 67, 68, 69, 70 };

 

String s1 = new String(ascii);

 

System.out.println(s1);

 

String s2 = new String(ascii, 2, 3); System.out.println(s2);

 

}

 

}

 

This program generates the following output:

 

ABCDEF

 

CDE

 

Extended versions of the byte-to-string constructors are also defined in which you can specify the character encoding that determines how bytes are converted to characters. However, you will often want to use the default encoding provided by the platform.

You can construct a String from a StringBuffer by using the constructor shown here: String(StringBuffer strBufObj)

 

You can construct a String from a StringBuilder by using this constructor: String(StringBuilder strBuildObj)

The following constructor supports the extended Unicode character set:

 

String(int codePoints[ ], int startIndex, int numChars)

 

Here, codePoints is an array that contains Unicode code points. The resulting string is constructed from the range that begins at startIndex and runs for numChars.

 

There are also constructors that let you specify a Charset.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.