Java String and char array

In this chapter you will learn:

  1. How to Get a single char by index
  2. How to copy part of a char array to String
  3. How to Get char array from string
  4. How to get char or char array from a string

Get a single char by index

To extract a single character from a String, you can refer directly to an individual character via the charAt() method. char charAt(int index) returns the char value at the specified index.

public class Main {
  public static void main(String[] argv) {
    byte[] bytes = new byte[]{67,68,69}; 
    // j  a v a2s  .  co m
    String str = new String(bytes);
    
    System.out.println(str.charAt(0));
  }
}

The output:

Copy part of a char array to String

We can create a String value by copying value from a char array.

  • static String copyValueOf(char[] data) returns a String that represents the character sequence in the array specified.
  • static String copyValueOf(char[] data, int offset, int count) returns a String that represents the character sequence in the array specified.
public class Main {
  public static void main(String[] argv) {
    char[] chars = new char[] { 'd', 'e', 'm', 'o', '2', 's', '.', 'c', 'o','m' };
/*  jav  a  2 s  .co m*/
    System.out.println(String.copyValueOf(chars));
  }
}

The output:

copyValueOf(char[] data, int offset, int count) copies value from a char array with offset.

public class Main {
/*j  ava 2s .  c  om*/
  public static void main(String[] arg) {
    char[] textArray = { 'T', 'o', ' ', 'b', 'e', ' ', 'o', 'r', ' ', 'n', 'o', 't', ' ', 't', 'o',
        ' ', 'b', 'e' };

    String text = String.copyValueOf(textArray, 9, 3);
    
    System.out.println(text);
  }

}

Get char array from string

char[] toCharArray() converts this string to a new character array.

import java.util.Arrays;
//j  ava2s .com
public class Main {
  public static void main(String[] argv) {
    String str = "java2s.com";
    char[] chars = str.toCharArray();
    System.out.println(Arrays.toString(chars));
  }
}

The output:

Get char array out of a sub string

If you need to extract more than one character at a time, you can use the getChars() method.

It has this general form:

void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
  • sourceStart specifies the index of the beginning of the substring
  • sourceEnd specifies an index that is one past the end of the desired substring.

The substring contains the characters from sourceStart through sourceEnd-1. The array that will receive the characters is specified by target. The index within target at which the substring will be copied is passed in targetStart.

To extract more than one character, use the getChars( ) method. The following program demonstrates getChars( ). It gets a sub set of the chars.

public class Main {
  public static void main(String args[]) {
//from   j a  va  2  s  . c o m
    String s = "This is a test string from java2s.com.";
    int start = 10;
    int end = 14;
    char buf[] = new char[end - start];

    s.getChars(start, end, buf, 0);
    System.out.println(buf);
  }
}

Here is the output of this program:

Next chapter...

What you will learn in the next chapter:

  1. How to Convert boolean, char, double, float,int,long,object to String
  2. How to Convert string case with locale
  3. How to encode a string into byte array
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