Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, November 16, 2021

Java - How to Convert Char To String?

1. Overview

In this tutorial, We'll learn how to convert char to string in java using Character.toString(), String.valueOf() and + operator with empty string.
Java - How to Convert Char To String?


2. Java Char to String Using Character.toString()


Character.toString(char) method takes the character as argument returns the same character as String format.

Character.toString() method returns a String with length or size exactly 1.

This method is available in java 1.4 onwards.

The below example is converting char to string java.
package com.javaprogramto.convert.chartostring;

/**
 * example to convert char to string in java using Character.toString() method.
 * 
 * @author javaprogramto.com
 *
 */
public class CharToStringExample1 {

	public static void main(String[] args) {

		// example 1

		char ch = 'c';

		String strC = Character.toString(ch);
		System.out.println("Example 1: ");
		System.out.println("Char in String : " + strC);
		System.out.println("returned string length : " + strC.length());

		// example 2

		char ch2 = 'z';

		String strZ = Character.toString(ch2);

		System.out.println("\nExample 2: ");
		System.out.println("Char in String : " + strZ);
		System.out.println("returned string length : " + strZ.length());

	}

}

Output:
Example 1: 
Char in String : c
returned string length : 1

Example 2: 
Char in String : z
returned string length : 1


3. Java Char to String Using String.valueOf(char)


String.valueOf() method takes the char as a parameter and returns a String with the char value.

valueOf() method always returns the string representation of char value and its length will be always 1.

Example:
package com.javaprogramto.convert.chartostring;

/**
 * example to convert char to string in java using String.valueOf() method.
 * 
 * @author javaprogramto.com
 *
 */
public class CharToStringExample2 {

	public static void main(String[] args) {

		// example 1
		char ch3 = 'x';

		String strC = String.valueOf(ch3);
		System.out.println("String.valueOf Example 1: ");
		System.out.println("Char in String : " + strC);
		System.out.println("returned string length : " + strC.length());

		// example 2
		char ch4 = '7';

		String strZ = String.valueOf(ch4);

		System.out.println("\nString.valueOf Example 2: ");
		System.out.println("Char in String : " + strZ);
		System.out.println("returned string length : " + strZ.length());

	}

}
Output:
String.valueOf Example 1: 
Char in String : x
returned string length : 1

String.valueOf Example 2: 
Char in String : 7
returned string length : 1

4. Java Char to String Using + operator with an empty string


You can use the + operator to add the char to the string which is empty.

+ operator can be used in the string concatenations.

First create the empty string literal "" and add the character to the string using + operator as below. This step will return the new string with only the character.
package com.javaprogramto.convert.chartostring;

/**
 * char to string using + operator
 * 
 * @author javaprogramto.com
 *
 */
public class CharToStringExample3 {

	public static void main(String[] args) {

		char ch3 = 'x';
		String returnedString = "" + ch3;
		System.out.println("Returned string 1 : " + returnedString);

		char ch1 = 'a';
		String returnedString2 = "" + ch1;
		System.out.println("Returned string 2 : " + returnedString2);

		char ch2 = 'b';
		String returnedString3 = "" + ch2;
		System.out.println("Returned string 3 : " + returnedString3);
	}
}

Output:
Returned string 1 : x
Returned string 2 : a
Returned string 3 : b

5. Conclusion


In this tutorial, We've seen converting char to string in java.





No comments:

Post a Comment

Please do not add any spam links in the comments section.