Java StringBuilder To Char Array

Java StringBuilder To Char Array | In this blog, we will convert the StringBuilder object to a char array. As we are trying to convert an object to a different data type so we cannot do it directly.

Example of Java StringBuilder to char array:-
StringBuilder: “Hello”
Char array: {‘H’, ‘e’, ‘l’, ‘l’, ‘o’}

The StringBuilder class contains getChars() method to convert StringBuilder to char array. The syntax of getChars() method is:- public void getChars(int start, int end, char[ ] destination, int deststart)

Parameters:-
Start: the index from where we need to start copying.
End: the index from where we need to end the copy.
Destination: the character array where we need to put all the copied elements.
Deststart: the index from where we need to paste the copied string.
Return type: void
Exception: throws StringIndexOutOfBoundException if the string is greater than the string length or lesser than 0.

Convert StringBuilder To Char Array Java

Let us see a program to convert StringBuilder to a char array in Java. While creating the array, we have to create an array greater or equal to the length of the StringBuilder or we should pass the “start”, and “end” values so that they don’t give IndexOutOfBoundsException.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Know Program");
      System.out.println("StringBuilder = " + sb);
      char[] arr = new char[sb.length()];
      sb.getChars(0, sb.length(), arr, 0);
      System.out.print("Char array: " + Arrays.toString(arr));
   }
}

Output:-

StringBuilder = Know Program
Char array: [K, n, o, w, , P, r, o, g, r, a, m]

In the below program, the array doesn’t have sufficient spaces to store the character elements therefore it throws IndexOutOfBoundsException.

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Know Program");
      System.out.println("StringBuilder = " + sb);
      char[] arr = new char[2];
      sb.getChars(0, sb.length(), arr, 0);
      System.out.print("Char array: " + Arrays.toString(arr));
   }
}

Output:-

StringBuilder = Know Program
Exception in thread “main” java.lang.IndexOutOfBoundsException: start 0, end 12, length 2

If we want to store only some portion of the StringBuilder to a char array then we can pass the “start” and “end” values based on our requirement. The below program demonstrate it:-

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Java Programming");
      System.out.println("String = " + string.toString());
      char[] arr = new char[15];

      Arrays.fill(arr, '_');
      string.getChars(5, 12, arr, 3);
      System.out.print("Char array: " + Arrays.toString(arr));
   }
}

Output:-

String = Java Programming
Char array: [ _, _, _, P, r, o, g, r, a, m, _, _, _, _, _ ]

Java StringBuilder To Char Array using String.getChars()

We also have another way to convert StringBuilder to a char array in Java. The String class also contains getChars() method which can be used to convert string to char array. Hence here we need to follow two steps:-

1. First convert the StringBuilder class to String.
2. Then use the getChars() method of the String class to convert the string to the char array. The syntax of getChars() method is:- public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Java Programming");
      String string = new String(sb);
      System.out.println("String = " + string);
      char[] arr = new char[string.length()];

      string.getChars(0, string.length(), arr, 0);
      System.out.print("Char array: " + Arrays.toString(arr));
   }
}

Output:-

String = Java Programming
Char array: [J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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