Java String Creation

In this chapter you will learn:

  1. How to create String object with String constructors
  2. How to create a string from byte array
  3. How to create a string from part of a byte array
  4. How to create String from char array
  5. How to create String from part of a char array
  6. How to create a String from another string

String Constructor

The String class supports several constructors.

  • String() from empty string
  • String(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:

  1. How to compare two String values
  2. How to compare two string value for equality
  3. What are the difference between equals() and ==
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words