Java – String to char conversion in 2 ways

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

Q) 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

1. 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

Read String class in detail with example

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

1.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));
		}

		// UPPER-CASE - 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));
		}

		// LOWER-CASE - 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

1.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

Q) 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

2. 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

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.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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