How to Convert a String to a Character in Java? Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a String to a char in Java.Example:In the below example, we have used the basic method i.e. charAt() method to convert each character in the string to a char array by iterating through the string. Java // Java program to convert a String to a Character // using the charAt() method public class StringToChar { public static void main(String[] args) { // Declare and initialize a String String s = "Java"; // Extract the first character from the String using charAt() method char c = s.charAt(0); // Get the character at index 0 System.out.println("Character at index 0: " + c); } } OutputCharacter at index 0: J Table of ContentOther Methods to Convert a String to a char in JavaUsing charAt() in a LoopUsing toCharArray( ) MethodUsing substring() MethodOther Methods to Convert a String to a char in JavaThere are 3 other methods using which we can convert a String to a char in Java.1. Using charAt() in a LoopWe can use the charAt() method in a loop to extract all characters from a string and then we can store them in a character array. Java // Java Program to Convert a String to Character Array // Using charAt() in a loop import java.util.Arrays; public class StringToChar { public static void main(String[] args) { String s = "Java"; char[] c = new char[s.length()]; // Extract each character using a loop for (int i = 0; i < s.length(); i++) { c[i] = s.charAt(i); } System.out.println("" + Arrays.toString(c)); } } Output[J, a, v, a] 2. Using toCharArray( ) MethodIn this example, the toCharArray() method converts the entire string into a character array. After this, we can extract individual characters.Example: Java // Java Program to convert a String to a char using toCharArray() import java.io.*; import java.util.Arrays; class StringToChar { public static void main(String[] args) { // Defining a string String s = "Java"; // Converting the string to a char array char[] arr = s.toCharArray(); System.out.println(Arrays.toString(arr)); } } Output[J, a, v, a] 3. Using substring() MethodWe can use the substring() method to extract a single character as a string. Then we can convert it to a char using charAt(0).Example: Java import java.util.*; public class SubstringExample { public static void main(String[] args) { // Defining a string String s = "Java"; // Extracting the character at index 1 using substring() char c = s.substring(1, 2).charAt(0); // Extract character at index 1 System.out.println("Character at index 1: " + c); } } OutputCharacter at index 1: a Comment More infoAdvertise with us Next Article How to Convert a String to Specific Character Encoding in Java? S sushantjarial Follow Improve Article Tags : Java Java Programs Java-Strings Java Examples Practice Tags : JavaJava-Strings Similar Reads How to Convert Char to String in Java? In this article, we will learn how to Convert Char to String in Java. If we have a char value like 'G' and we want to convert it into an equivalent String like "G" then we can do this by using any of the following three listed methods in Java. Using toString() method of Character classUsing valueOf( 5 min read Convert a String to Character Array in Java Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E 2 min read Convert a String to Character Array in Java Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E 2 min read How to Convert a String to an Long in Java ? In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210 2 min read How to Convert a String to Specific Character Encoding in Java? In Java, the process of converting a String to a certain character encoding is converting the string's character sequence to a byte array and applying the chosen character encoding. Java Program to Convert a String to a Specific Character EncodingUsing the String class's getBytes() function to trans 2 min read How to Convert a String to an Numeric in Java? In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin 2 min read Like