String to char conversion in Java

In this article, we will discuss various ways to convert String to char in Java

 

What is the need of converting String to primitive char or Character wrapper-type ?

  • Generally, whenever we receive any data from web application then it is passed in the form of String only
  • To use char value in Java application, it needs to be converted into Character first and then we can use accordingly depending upon business requirements
  • This article explains about String to primitive char or Character wrapper-type conversion only, but we can do conversion for other types like int, double, float, long, boolean, etc
  • Note: Likewise, sometime Character to String conversion is also required

 

Various ways to convert String to Character

  1. Iterate through String using regular for-loop and get character at each position/index using charAt(index) method
  2. Assign single char value to String directly using charAt(0) method

 

Auto-boxing and un-boxing feature from Java 1.5 version:

  • charAt() method returns primitive char data-type, but it can be easily used as Character wrapper-type
  • as auto-boxing feature helps to convert primitive data-type to respective wrapper-types
  • Newly created Character wrapper-type object can be easily converted to primitive char data-type
  • let’s see one example on this auto-boxing feature, at the very end

 

Read String class in detail with example

 

Let us move forward and discuss all possible ways to convert String to Character in Java

Way 1: Iterate through String using regular for-loop and get character at each position/index using charAt(index) method

  • This method can be used to convert String into primitive char data-type
  • In this approach, for each iteration we will get single character with index-position

Method signature:

public char charAt(int index);

 

ConvertStringIntoCharacterUsingForLoop.java

package in.bench.resources.string.to.character.conversion;

public class ConvertStringIntoCharacterUsingForLoop {

	public static void main(String[] args) {

		// String
		String str1 = "Bench";
		System.out.println("Ex 1. Sample string 'Bench'\n");

		// 1. converting String to char
		for(int index = 0; index < str1.length(); index ++) {

			// retrieving each char using charAt(index)
			System.out.println("char value at " + index + 
					" index-position is : " + str1.charAt(index));
		}


		// String with all upper-case
		String str2 = "GOOGLE";
		System.out.println("\nEx 2. All upper-case string 'GOOGLE'\n");

		// 2. converting String to char
		for(int index = 0; index < str2.length(); index ++) {

			// retrieving each char using charAt(index)
			System.out.println("char value at " + index + 
					" index-position is : " + str2.charAt(index));
		}


		// String with all lower-case
		String str3 = "oracle";
		System.out.println("\nEx 3. All lower-case string 'oracle'\n");

		// 3. converting String to char
		for(int index = 0; index < str3.length(); index ++) {

			// retrieving each char using charAt(index)
			System.out.println("char value at " + index + 
					" index-position is : " + str3.charAt(index));
		}
	}
}

Output:

Ex 1. Sample string 'Bench'

char value at 0 index-position is : B
char value at 1 index-position is : e
char value at 2 index-position is : n
char value at 3 index-position is : c
char value at 4 index-position is : h

Ex 2. All upper-case string 'GOOGLE'

char value at 0 index-position is : G
char value at 1 index-position is : O
char value at 2 index-position is : O
char value at 3 index-position is : G
char value at 4 index-position is : L
char value at 5 index-position is : E

Ex 3. All lower-case string 'oracle'

char value at 0 index-position is : o
char value at 1 index-position is : r
char value at 2 index-position is : a
char value at 3 index-position is : c
char value at 4 index-position is : l
char value at 5 index-position is : e

 

Way 2: Assign single character of String to char value directly using charAt(0) method

  • This method converts String consisting of single character into primitive char data-type
  • Here, directly accessing index 0 using charAt() method because we are sure that there is only one character present in the string (unlike previous way-1)

Method signature:

public char charAt(int index);

 

ConvertStringIntoCharacterUsingCharAtMethod.java

package in.bench.resources.string.to.character.conversion;

public class ConvertStringIntoCharacterUsingCharAtMethod {

	public static void main(String[] args) {

		// String with single character in upper-case
		String str1 = "A";

		// 1. converting String to char
		char chValue1 = str1.charAt(0);
		System.out.println("1. Converted upper-case char value is : " + chValue1);


		// String with single character in upper-case
		String str2 = "x";

		// 2. converting String to char
		char chValue2 = str2.charAt(0);
		System.out.println("\n2. Converted lower-case char value is : " + chValue2);
	}
}

Output:

1. Converted upper-case char value is : A

2. Converted lower-case char value is : x

 

What if we want to convert primitive char data-type to Character wrapper-type or vice-versa ?

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing feature

AutoBoxingFeatureForCharConversion.java

package in.bench.resources.string.to.character.conversion;

public class AutoBoxingFeatureForCharConversion {

	public static void main(String[] args) {

		// String with upper-case
		String str1 = "C";

		// converting String to char
		char chValue1 = str1.charAt(0);

		// 1. Auto-Boxing - converting char to Character
		Character chAutoBoxing = chValue1;
		System.out.println("1. Auto-Boxing : " + chAutoBoxing);


		// String with lower-case
		String str2 = "z";

		// converting String to Character
		Character chValue2 = new Character(str2.charAt(0));

		// 2. Un-Boxing - converting Character to char
		char chUnBoxing = chValue2;
		System.out.println("\n2. Un-Boxing   : " + chUnBoxing);
	}
}

Output:

1. Auto-Boxing : C

2. Un-Boxing   : z

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
https://docs.oracle.com/javase/tutorial/java/data/converting.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
https://docs.oracle.com/javase/6/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

 

Happy Coding !!
Happy Learning !!

Character to String conversion in Java - 6 ways
Boolean to String conversion in Java - 6 ways