Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to Convert String to Char in Java

Last Updated on April 18, 2023 by Prepbytes

In Java, String and char are two different data types, and sometimes we need to convert Strings to char or char to String. Converting String to char involves extracting individual characters from the string, which can be done in multiple ways.

Converting String to char in Java

We can easily convert String to char in Java by using the built-in Functions. The String and char in Java are defined as:

  • char: char is a primitive data type in Java that is used to represent a single character.
  • String: A String in Java is an object which is used to store a sequence of characters.

We can convert String to char in Java by using the following two methods.

  • charAt()
  • toCharArray()

Let’s discuss both of these in detail.

Method 1 to Convert String to Char in Java: Using charAt() Method

The String Class in Java contains the “charAt()” method. This method is used to return the character at a particular location in a string. To convert a string to a char in Java, we can extract the first character of the string using the charAt() method as shown in the example given below.

Example 1 of Converting String to Char in Java using charAt() method

This example uses the charAt() method to convert the String to char in Java.

Code:

class PrepBytes{
    public static void main(String args[]){
        String str = "PrepBytes";
        char ch = str.charAt(0);
        System.out.println("The character at first index is " + ch);
    }
}

Output:

The character at first index is P

Explanation:
In the above example, we declared a String str and initialized it with the value “PrepBytes”. Then, we extracted the first character i.e., ‘P’ by using the charAt() method and printed it on the output screen.

Example 2 of Converting String to Char in Java using charAt() method

This example will convert the entire String into characters with the help of for loop and the charAt() method.

Code:

class PrepBytes{
    public static void main(String args[]){
        String str = "PrepBytes";
        for(int i=0; i<str.length(); i++){
            char ch = str.charAt(i);
            System.out.println("The character at index " + i + " is " + ch);
        }
    }
}

Output:

The character at index 0 is P
The character at index 1 is r
The character at index 2 is e
The character at index 3 is p
The character at index 4 is B
The character at index 5 is y
The character at index 6 is t
The character at index 7 is e
The character at index 8 is s

Explanation:
In the above code, we have first declared a string and initialized it with “PrepBytes”. Next, we used a for loop to traverse the entire string and within the for loop to extract the character one by one and printed each character along with its position on the output screen.

Method 2 to Convert String to Char in Java: Using toCharArray() Method

We can also convert the entire string in Java into a Character Array with the help of toCharArray() method. This method is also defined in the String class in Java.

Let us learn how to use this method in Java Code for converting the entire string into an array of characters with the help of the following example.

Example to Convert String to char in Java

The following code illustrates the usage of the toCharArray() method in Java.

Code:

class PrepBytes{
    public static void main(String args[]){
        String str = "PrepBytes";
        
        //using the toCharArray() method
        char[] ch = str.toCharArray();

        for (char i : ch) {
            System.out.println("The Character is " + i);
        }
    }
}

Output:

The Character is P
The Character is r
The Character is e
The Character is p
The Character is B
The Character is y
The Character is t
The Character is e
The Character is s

Explanation:
In this code, we have declared a string “PrepBytes” and then used the toCharArray() method to convert the string into a character array. The toCharArray() method returns a character array that is stored with the name “ch”. Finally, we printed all the elements of the character array with the help of a for-each loop.

Conclusion
In this article, we have learned How we can convert String to char in Java with the help of charAt() and toCharArray() methods. We have also discussed examples for a better understanding of both methods. The charAt() method extracts a character at a specified index whereas the toCharArray() method converts the entire string into the Character Array. Depending on the requirements, we can use any of the two methods to convert String to char in Java.

Frequently Asked Questions (FAQs)

Some Frequently Asked Questions related to “String to char in Java” are listed below.

Ques 1. What is the return type of charAt() method?
Ans. The return type of the charAt() method is char.

Ques 2. Can I use the charAt() method to convert a String with multiple characters to a char?
Ans. No, you cannot use the charAt() method to convert a String with multiple characters to a char. The charAt() method only returns a single character. To convert we need to run a for loop to extract each character one by one.

Ques 3. What is the return type of toCharArray() method in Java?
Ans. The return type of the toCharArray() method is char[].

Ques 4. Can I use the substring() method to convert a String to a char?
Ans. No, you cannot use the substring() method to convert a String to a char. The substring() method returns a String, not a char.

Ques 5. How can I convert a String to a char using the valueOf() method?
Ans. You can convert a String to a char using the valueOf() method of the Character class. The valueOf() method returns a Character object that represents the specified char value.

Leave a Reply

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