String Constructor
The String class supports several constructors.
String()
- Creates empty string
String(byte[] bytes)
- Creates a String from specified array of bytes using the platform's default charset.
String(byte[] bytes, Charset charset)
- Creates a String by decoding the bytes using the specified charset.
String(byte[] bytes, int offset, int length)
- Creates a String by decoding the subarray of bytes using the platform's default charset.
String(byte[] bytes, int offset, int length, Charset charset)
- Creates a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, int offset, int length, String charsetName)
- Creates a String by decoding the subarray of bytes using the specified charset.
String(byte[] bytes, String charsetName)
- Creates a String by decoding the array of bytes using the specified charset.
String(char[] value)
- Creates a String from the character array argument.
String(char[] value, int offset, int count)
- Creates a new String from a subarray of char array argument.
String(int[] codePoints, int offset, int count)
- Creates a new String that contains characters from a subarray of the Unicode code point array argument.
String(String original)
- Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
String(StringBuffer buffer)
- Creates a new string that contains the sequence of characters currently contained in the string buffer argument.
String(StringBuilder builder)
- Creates a new string that contains the sequence of characters currently contained in 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");
// 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);
}
}
new String(byte[] bytes)
Initialize a string with given a byte array.
public class Main {
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);
}
}
new String(byte[] ascii, int hibyte)
public class MainClass {
static byte a[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
static byte b[] = { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };
public static void main(String args[]) {
System.out.println("a = " + new String(a, 0));//a = ABCDEFGHIJ
}
}
The following code creates a string from sub byte array.
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 };
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii, 2, 3);
System.out.println(s2);
}
}
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");
// 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);
}
}
Creating string from part of a char array
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");
// 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);
}
}
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");
// 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);
}
}
Home
Java Book
Essential Classes
Java Book
Essential Classes
String:
- String type and Literals
- String Concatenation
- String.CASE_INSENSITIVE_ORDER
- String Constructor
- charAt(int index):Get a single char by index
- String: compareTo(String stringValue)
- concat(String str)
- equals():Compare two string value for equality
- equals( ) vs ==
- contains(CharSequence s)
- copyValueOf(char[] data)
- endsWith(String suffix)
- format():Format a string
- getBytes():Get byte array from string
- getChars()
- indexOf
- intern a string
- isEmpty:if string is empty
- lastIndexOf()
- length() Returns the length of this string
- startsWith( )
- toLowerCase() and toUpperCase(): convert string case with locale
- substring:Get sub string from a string
- toCharArray():Get char array from string
- toString( )
- trim()
- valueOf():Convert boolean, char, double, float,int,long,object to String