How to convert a char to a String

0 votes
I have a char and I need a String. How do I convert from one to the other?
Dec 29, 2020 in Java by anonymous
• 8,910 points
1,161 views

4 answers to this question.

0 votes

You can use Character.toString(char). Note that this method simply returns a call to String.valueOf(char), which also works.

As others have noted, string concatenation works as a shortcut as well:

String s = "" + 's';

But this compiles down to:

String s = new StringBuilder().append("").append('s').toString();

which is less efficient because the StringBuilder is backed by a char[] (over-allocated by StringBuilder() to 16), only for that array to be defensively copied by the resulting String.

String.valueOf(char) "gets in the back door" by wrapping the char in a single-element array and passing it to the package private constructor String(char[], boolean), which avoids the array copy.

answered Dec 29, 2020 by Gitika
• 65,890 points
0 votes

Convert Char To String

  1. public class CharToString_toString {
  2. public static void main(String[] args) {
  3. //input character variable.
  4. char myChar = 'g';
  5. //Using toString() method.
  6. //toString method take character parameter and convert string.
  7. String myStr = Character. toString(myChar);
  8. //print string value.
answered Dec 29, 2020 by Nikita
0 votes

Java char to String Example: String.valueOf() method

Let's see the simple code to convert char to String in java using String.valueOf() method.

  1. char c='S';  
  2. String s=String.valueOf(c);  

Let's see the simple example of converting char to String in java.

public class CharToStringExample1{  

public static void main(String args[]){  

char c='S';  

String s=String.valueOf(c);  

System.out.println("String is: "+s);  

}}  
  1. Output:

    String is: S
answered Dec 29, 2020 by Carlos
0 votes

There are two ways you can do char to String conversion –
1. Using String.valueOf(char ch) method
2. Using Character.toString(char ch) method

answered Dec 29, 2020 by Roshni
• 10,520 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,224 views
0 votes
1 answer

How to remove ‘char’ from a String?

Hi...here you can try using the overloaded ...READ MORE

answered May 22, 2018 in Java by code.reaper12
• 3,500 points
660 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,218 views
0 votes
2 answers

How to convert a java string into byte array

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered Aug 22, 2019 in Java by Sirajul
• 59,230 points
1,412 views
0 votes
1 answer

Why is char[] preferred over a string?

Strings are immutable. That means once you've ...READ MORE

answered Jun 14, 2018 in Java by scarlett
• 1,290 points
621 views
0 votes
0 answers

String to char array

How to convert a string to a ...READ MORE

Nov 3, 2023 in Java by Arya
• 990 points
289 views
0 votes
1 answer

How can two strings be concatenated in java?

You can concatenate Strings using the + operator: System.out.println("Your number ...READ MORE

answered Jun 6, 2018 in Java by Daisy
• 8,140 points
791 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
5,495 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,264 views
0 votes
3 answers

How to check if a String is numeric in Java?

Check if a string is numeric public class ...READ MORE

answered Dec 29, 2020 in Java by Carlos
2,742 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP