Java String Creation
In this chapter you will learn:
- How to create String object with String constructors
- How to create a string from byte array
- How to create a string from part of a byte array
- How to create String from char array
- How to create String from part of a char array
- How to create a String from another string
String Constructor
The String class supports several constructors.
String()
from empty stringString(byte[] bytes)
from byte array using default charset.String(byte[] bytes, Charset charset)
from the bytes using the specified charset.String(byte[] bytes, int offset, int length)
decoding the subarray of bytes using the platform's default charset.String(byte[] bytes, int offset, int length, Charset charset)
decoding the specified subarray of bytes using the specified charset.String(byte[] bytes, int offset, int length, String charsetName)
decoding the subarray of bytes using the specified charset.String(byte[] bytes, String charsetName)
decoding the array of bytes using the specified charset.String(char[] value)
from the character array argument.String(char[] value, int offset, int count)
from a subarray of char array argument.String(int[] codePoints, int offset, int count)
from a subarray of the Unicode code point array argument.String(String original)
creates a copy of the argument string.String(StringBuffer buffer)
from the string buffer argument.String(StringBuilder builder)
from the string builder argument.
To create an empty String, you call the default constructor.
String s = new String();
will create an instance of String with no characters in it.
public class Main {
public static void main(String args[]) {
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ',
'd', 'a', 'y' };
String s = new String("hello");
/*from ja va2 s. c o m*/
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n",
s1, s2, s3, s4);
}
}
The output:
Create a string from byte array
new String(byte[] bytes)
initializes a string with given a byte array.
public class Main {
public static void main(String args[]) {
byte ascii[] = { 65, 66, 67, 68, 69, 70 };
/* j ava 2 s . co m*/
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
The output:
Create a string from part of a byte array
The following code creates a string from sub byte array with
new String(byte[] bytes, int offset, int length).
public class Main {
public static void main(String args[]) {
byte ascii[] = { 65, 66, 67, 68, 69, 70 };
/* j av a2s . c o m*/
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
Create a string from char array
To create a String initialized by an array of characters: new String(char[] value)
public class Main {
public static void main(String args[]) {
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
String s = new String("hello");
/* j av a 2 s . co m*/
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
Create String from part of a char array
Creating string from part of a char array with
new String(char[] value, int offset, int count)
.
public class Main {
public static void main(String args[]) {
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
String s = new String("hello");
// ja v a 2 s . c o m
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
Create a String from another string
Construct a String object that contains the same character sequence as
another String object using this constructor:new String(String original)
public class Main {
public static void main(String args[]) {
char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
String s = new String("hello");
/*from j a v a 2 s.c om*/
// use String constructors
String s1 = new String();
String s2 = new String(s);
String s3 = new String(charArray);
String s4 = new String(charArray, 6, 3);
System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to compare two String values
- How to compare two string value for equality
- What are the difference between equals() and ==