Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

String is a common type, and char is a primitive in Java.

In this tutorial, we’ll explore how to convert a String object to char in Java.

2. Introduction to the Problem

We know that a char can only contain one single character. However, a String object can contain multiple characters.

Therefore, our tutorial will cover two cases:

  • The source is a single-character string.
  • The source is a multi-character string.

For case 1, we can get the single character as a char easily. For example, let’s say this is our input:

String STRING_b = "b";

After the conversion, we expect to have a char ‘b‘.

For case 2, if the source String is a multi-character string and we still want to get one single char as a result, we must analyze the requirement to pick the desired character, such as the first, the last, or the n-th character.

In this tutorial, we’ll address a more general solution. We’ll convert the source String to a char array that holds each character in the string. In this way, we can pick any elements according to the requirement.

We’ll use STRING_Baeldung as the input example:

String STRING_Baeldung = "Baeldung";

So next, let’s see them in action.

3. Single Character String

Java’s String class provides the charAt() to get the n-th character (0-based) from the input string as char. Therefore, we can directly call the method getChar(0) to convert a single character String to a char:

assertEquals('b', STRING_b.charAt(0));

However, we should note that if the input is an empty string, the charAt() method call throws StringIndexOutOfBoundsException:

assertThrows(StringIndexOutOfBoundsException.class, () -> "".charAt(0));

Therefore, we should check the input string is not null or empty before we call the charAt() method.

4. Multiple Characters String

We’ve learned to use charAt(0) to convert a single character String to char. If the input is a multi-character String, and we know exactly which character we want to be converted to a char, we can still use the charAt() method. For example, we can get the fourth character (‘l‘) from the input string “Baeldung“:

assertEquals('l', STRING_Baeldung.charAt(3));

Additionally, we can use String.toCharArray() to get a char[] array containing all the characters:

assertArrayEquals(new char[] { 'B', 'a', 'e', 'l', 'd', 'u', 'n', 'g' }, STRING_Baeldung.toCharArray());

It’s worth mentioning that the toCharArray() method also works for empty string inputs. It returns an empty char array as the result:

assertArrayEquals(new char[] {}, "".toCharArray());

Apart from toCharArray(), String.getChars() can extract consecutive characters from the given String to a char array. The method receives four arguments:

  • srcBegin – the index of the first character in the string to take, inclusive
  • srcEnd – the index of the last character in the string to copy, exclusive
  • dst – the destination array, which is our result
  • dstBegin – the start offset in the destination array. We’ll discuss this with an example.

First, let’s extract “aeld” from the string “Baeldung” and fill it into a predefined char array:

char[] aeld = new char[4];
STRING_Baeldung.getChars(1, 5, aeld, 0);
assertArrayEquals(new char[] { 'a', 'e', 'l', 'd' }, aeld);

As the test above shows, to call getChars(), we should first have a char array to hold the result.

In the example, we pass 0 for dstBegin when we call getChars(). This is because we’d like the converted result to start from the first element in the array aeld.

Of course, sometimes, we want the result to overwrite a middle part of the array. Then we can set the dstBegin to the desired value.

Next, let’s see another example to convert “aeld” to chars and overwrite the target array from the second (index=1) element:

char[] anotherArray = new char[] { '#', '#', '#', '#', '#', '#' };
STRING_Baeldung.getChars(1, 5, anotherArray, 1);
assertArrayEquals(new char[] { '#', 'a', 'e', 'l', 'd', '#' }, anotherArray);

So, as we can see, we pass dstBegin=1 to the method and get the expected result.

5. Conclusion

In this article, we’ve learned how to convert a String to a char in Java.

As always, the code used in the article is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.