Java String toCharArray() method

Let’s learn java String toCharArray() method.

Java String toCharArray() method

Java String toCharArray() method converts given string into new character array.

java String toCharArray() method

Syntax

public char[ ] toCharArray()

Return

returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Also read – string methods in java

Let’s see java String toCharArray() method example or string to char array java.

public class StringToCharArrayDemo
{
   public static void main(String[] args)
   {
      String strInput = new String("Flower Brackets");
      // using toCharArray() method java
      char[] chArray = strInput.toCharArray();
      System.out.println("Character array: ");
      for(char ch : chArray)
      {
         System.out.println(ch);
      }
   }
}


Output:

Character array:
F
l
o
w
e
r

B
r
a
c
k
e
t
s


char to string java

Let’s learn to convert char to string java using Character.toString() method.

Character.toString() method returns a String object representing the specified char.

Also read – string length in java

The result is a string of length1 consisting solely of the specified char.

Let’s see a simple example on char to string java.

public class CharToString
{
   public static void main(String[] args)
   {
      char ch ='G';
      String str = Character.toString(ch);
      System.out.println("char to string java is: " + str);
   }
}


Output:

char to string java is: G


We can also use String.valueOf(char c) method to convert char to string java.

This method returns a string of length 1 containing as its single character the argument c. Let’s see an example.

public class CharToStringExample
{
   public static void main(String[] args)
   {
      char ch = 'F';
      String str = String.valueOf(ch);
      System.out.println("After conversion is: " + str);
   }
}


Output:

After conversion is: F


charat in java

charAt() method in java returns the char value at the specified index. An index ranges from 0 to length() – 1.

Also read – reverse a string in java

The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Syntax:

public char charAt(int index)

Parameters:

index – the index of the char value.

Returns:

the char value at the specified index of this string. The first char value is at index 0.

Throws:

IndexOutOfBoundsException – if the index argument is negative or not less than the length of this string.

Now let’s see java String charAt() method example.

public class CharAtMethodJava
{
   public static void main(String[] args)
   {
      String strInput = "Hello World Java";
      char ch1 = strInput.charAt(1);
      char ch2 = strInput.charAt(6);
      char ch3 = strInput.charAt(14);
      System.out.println("Character at 1st index is: " + ch1);
      System.out.println("Character at 6th index is: " + ch2);
      System.out.println("Character at 14th index is: " + ch3);
   }
}


Output:

Character at 1st index is: e
Character at 6th index is: W
Character at 14th index is: v

Reference: oracle docs