How to break a string into characters in Java?

In this tutorial, we will learn how to break(or split) a String into characters. The end result can be an array of characters or an array of Strings which contain characters. Below are the introduction and the method to solve the problem-

Given a string, the task here is to break the string into characters.
A String is a sequence of characters. But unlike C++, Strings in Java are not mutable. Which means that Strings cannot be changed or modified. This is one reason why we need the following methods to get all the characters in the String.

So, to obtain all the characters present in the String, there are 3 ways to do that:
1. By using Split() method.
2. By converting our string into a character array.
3. By using CharAt() method.

Using the Split method

This is the simplest way to obtain all the characters present in the String. The Split method, as the name suggests, splits the String against the given regular expression.
There is a small change in the way of splitting Strings from Java 8 and above.

Java 8 and above (most commonly used):

//Java program to show the use of Split method (Java 8 and above)
public class SplitString{

  public static void main(String[] args) 
  {
    //Initialize the String which needs to be split
    String str = "Enlighter";
    
    //Use the Split method and store the array of Strings returned in a String array.
    String[] arr = str.split("");
    
    //Printing the characters using for-each loop
    for(String character : arr)
      System.out.print(char

Output –

E n l i g h t e r

Before Java 8 :

The only small change that needs to be done is, we have to give a regular-expression inside the parameters of the Split method. The regular-expression (regex) that we pass is known as negative lookahead. You can check the documentation here.

//Java program to show the use of Split method (Before Java 8)

public class SplitString {

  public static void main(String[] args) 
  {
    //Initialize the String which needs to be split
    String str = "Enlighter";
    
    //Use the Split method and store the array of Strings returned in a String array.
    String[] arr = str.split("(?!^)");
    
    //Printing the characters using for-each loop
    for(String character : arr)
      System.out.print(character + " ");
  }

}

Output – 

E n l i g h t e r

Note : We can also use System.out.Println(character) to print each character on different lines.

Converting the given string into a character array

This way involves copying the characters into an array. This takes almost the same computation time as the first method because both of these methods involve copying from one place to another.
We use toCharArray(), which converts the given string into an array of characters.

public class Example {

  public static void main(String[] args) {
    
    //Initialize a test String
    String test = "Hello World, CodeSpeedy here";
    
    //Using the toCharArray method and storing the returned character array
    char[] stringToChar = test.toCharArray();
    
    //Printing the characters
    for (char output : stringToChar) 
      System.out.print(output + " ");

  }

}

Output –

H e l l o   W o r l d ,   C o d e S p e e d y   h e r e

Note : We can also use System.out.Println(character) to print each character on different lines.

Making use of charAt() method

This way is faster than the first two because this does not involve copying from the String to an array. Here, in this method, we store each character in the character array. To do that, we use charAt() method. This method returns the character present at the position that is passed as parameter to the method.
The program below shows the implementation of charAt() method to get all the characters in the given method.

public class Example {

  public static void main(String[] args) {
    
    //Initialize a test String
    String test = "Hello World, CodeSpeedy here";

    //Store the length in a integer variable
    int length = test.length();
    
    //Character array to store the characters
    //of length same as the string = 'length'
    char[] arr = new char[length];
    
    //loop to store each character in the array and then print the same
    for (int i=0; i<length; i++) 
    {
      arr[i] = test.charAt(i);
        System.out.print(arr[i] + " ");
    }

  }

}

Output –
The output of our program is given below:

H e l l o   W o r l d ,   C o d e S p e e d y   h e r e

Note :  In the above code, the characters returned can be stored in an array of String too. That can be done so by declaring an array of String on line number 9 and then by doing the following changes on line number 14 

//Make sure that 'arr' is declared as an array of type String
arr[i] = Character.toString(test.charAt(i));

 

Related posts :

Leave a Reply

Your email address will not be published. Required fields are marked *