How to Convert a String to a Character in Java? Last Updated : 20 Nov, 2024 Comments Improve Suggest changes 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 a Character 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 How to Convert a String to ArrayList in Java? Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet 1 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 URL in Java? In Java, a string is a sequence of characters. A URL, which is short for Uniform Resource Locator, refers to a web resource that specifies its location on a computer network and provides the mechanism for retrieving it. To represent a URL in Java, we use the java.net.URL class. In this article, we w 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 How to Convert a String Class to an Integer Class in Java? String in Java is used to store the sequence of characters in Java. A string can contain a character, a word, a sentence, or a paragraph. Integer is a Wrapper class that is used to store Integer values in Java. The range of the Integer class is from -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31 - 1 3 min read Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St 4 min read How to find the first and last character of a string in Java Given a string str, the task is to print the first and the last character of the string. Examples: Input: str = "GeeksForGeeks" Output: First: G Last: s Explanation: The first character of the given string is 'G' and the last character of given string is 's'. Input: str = "Java" Output: First: J Las 3 min read Like