Katerina Zamani

About Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.

Java String to char Example

String to char conversion is a very simple procedure in Java. This process is useful when we want to use char format instead of strings in our applications, for instance in the arguments in command line.

In this example we are going to show you how to parse a String to character(s).

1. Example of String to char conversion

Create a new java file with the name StringToCharClass and paste the following code.

 

StringToCharClass.java:

package com.javacodegeeks.basics.stringtochar;

import java.util.Arrays;

public class StringToCharClass {

	public static void main(String[] args) {
		String mystr = "JCG Examples";
		
		char[] charArray = mystr.toCharArray();
		System.out.println("mystr in character array: " + Arrays.toString(charArray));

		// handle the char array
		for(int i = 0; i < charArray.length; i++) {
			System.out.print(charArray[i] + " - ");
		}
		
		// take a char (letter) in a specific position
		char firstLetter = mystr.charAt(0);
		System.out.println("\nFirst letter of mystr: " + firstLetter);
		
		// take every char of the string 
		for(int i = 0; i < mystr.length(); i++) {
			System.out.println("Char " + mystr.charAt(i) + " in position " + i);
		}
		
	}

}

Now lets explain the code above. We can convert a String to an array of characters, with the use of toCharArray() operation. So, the resulting array includes one char of the specified string in each position. In order to handle the values of the array we can use a for-loop, but for printing the array in a readable way, we can simply call Arrays.toString() method.

To get a char in a particular position of the string, we can use charAt() function by defining the specified index. As you can imagine, charAt() can be called into a for-loop, in order to get all the chars of the string. In the example you can notice that we use that for-loop, where the index reaches the length of the string.

Now you can see the results of the execution of the source code.

Output:

mystr in character array: [J, C, G,  , E, x, a, m, p, l, e, s]
J - C - G -   - E - x - a - m - p - l - e - s - 
First letter of mystr: J
Char J in position 0
Char C in position 1
Char G in position 2
Char   in position 3
Char E in position 4
Char x in position 5
Char a in position 6
Char m in position 7
Char p in position 8
Char l in position 9
Char e in position 10
Char s in position 11

Download the source code

This was an example of string to char in Java. Download the source code of this example: StringToCharExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close