Create string objects

ConstructorSummary
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.

Initialize a string with given a byte array.


public class Main {
  public static void main(String[] argv) {
    byte[] bytes = new byte[]{67,68,69}; 
    
    System.out.println(new String(bytes));
  }
}

The output:


CDE

The following code creates a string from sub 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);
  }
}

ABCDEF
CDE

To create a String initialized by an array of characters:

 
public class Main {
  public static void main(String[] argv) {
    char chars[] = {'a', 'b', 'c'};
    String s = new String(chars);

    System.out.println("s =" + s);
  }
}

s =abc

Creating string from part of a char array

 
public class Main {
  public static void main(String[] argv) {
    char chars[] = {'j', 'a', 'v', 'a', '2', 's'};
    String s = new String(chars, 2, 3);
    System.out.println("s =" + s);
  }
}

s =va2

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:

 
public class Main {

  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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.