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.

Answer from Paul Bellora on Stack Overflow
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ convert-char-to-string-java
Convert char to String in Java | DigitalOcean
August 4, 2022 - Letโ€™s look at the two methods to convert char array to string in java program. You can use String(char[] value) constructor to convert char array to string.
Top answer
1 of 14
703

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.

2 of 14
278

I've got of the following five six methods to do it.

// Method #1
String stringValueOf = String.valueOf('c'); // most efficient

// Method #2
String stringValueOfCharArray = String.valueOf(new char[]{x});

// Method #3
String characterToString = Character.toString('c');

// Method #4
String characterObjectToString = new Character('c').toString();

// Method #5
// Although this approach seems very simple, 
// this is less efficient because the concatenation
// expands to a StringBuilder.
String concatBlankString = 'c' + "";

// Method #6
String fromCharArray = new String(new char[]{x});

Note: Character.toString(char) returns String.valueOf(char). So effectively both are same.

String.valueOf(char[] value) invokes new String(char[] value), which in turn sets the value char array.

public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}

On the other hand String.valueOf(char value) invokes the following package private constructor.

String(char[] value, boolean share) {
    // assert share : "unshared not supported";
    this.value = value;
}

Source code from String.java in Java 8 source code

Hence String.valueOf(char) seems to be most efficient method, in terms of both memory and speed, for converting char to String.

Sources:

  1. How to convert primitive char to String in Java
  2. How to convert Char to String in Java with Example
Discussions

How to convert string to char array in C++? - Stack Overflow
I would like to convert string to char array but not char*. I know how to convert string to char* (by using malloc or the way I posted it in my code) - but that's not what I want. I simply want to More on stackoverflow.com
๐ŸŒ stackoverflow.com
how to convert char * to string
Hi Does anyone know how I could convert char *variable for exemple char *message="Hello"; to a string, to have string messconv="Hello"; after doing a conversion ? Thank you More on forum.arduino.cc
๐ŸŒ forum.arduino.cc
0
1
December 29, 2017
How do I convert a char to String?
I am converting data from a keypad. How do I convert a char to a String easily? Also, is there any way to stop an Arduino until it receives input from a keypad? More on google.com
๐ŸŒ google.com
0
0
October 20, 2013
A Simpler Way to Convert a String to char*?
Hi all I am using an Arduino Function that returns a String object. I need to send that string to another function, which accepts a char Array. I tried simply sending the String object, in the hope that some implicit conversion will happen, but none did.. So I wrote this small helper function: ... More on forum.arduino.cc
๐ŸŒ forum.arduino.cc
0
0
September 24, 2019
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
how to convert char * to string - Programming - Arduino Forum
December 29, 2017 - Hi Does anyone know how I could convert char *variable for exemple char *message="Hello"; to a string, to have string messconv="Hello"; after doing a conversion ? Thank you
Find elsewhere
๐ŸŒ
Google
google.com โ€บ projects โ€บ programming
How do I convert a char to String? - Programming - Arduino Forum
October 20, 2013 - They stay put and don't do anything you didn't code, like making a copy + 1 char then deleting the old copy just because you coded myword += "X". There are very good C Strings Tutorials on the web, just Google those words. Also Arduino is made to do real-time code.
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
A Simpler Way to Convert a String to char*? - Programming - Arduino Forum
September 24, 2019 - Hi all I am using an Arduino Function that returns a String object. I need to send that string to another function, which accepts a char Array. I tried simply sending the String object, in the hope that some implicit conversion will happen, but none did.. So I wrote this small helper function: char* ConvertStringToCharArray(String S) { int ArrayLength =S.length()+1; //The +1 is for the 0x00h Terminator char CharArray[ArrayLength]; S.toCharArray(CharArray,ArrayLength); retur...
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ char to std::string conversion
r/cpp_questions on Reddit: Char to std::string conversion
May 23, 2024 -

Unfortunately for C++ programmers all over the world, there doesn't exist a std::string constructor that simply takes a char, so you're not able to do something like std::string test('A') (does anyone have an explication for this?).

Of course, this leads to super frustrating / non-intuitive behavior and introduces tons of hidden bugs, but it is what it is.

However, I recently discovered the following code snippets, and for the life of me I cannot understand why this works this way, and I was wondering if someone could explain the rationale behind this (tested of both gcc and clang).

If you write :

std::string test = "STRING";
std::string test2 = test[0];

You, (of course, anyone would have guessed I'm sure) get a compilation error, because test[0] returns a char which cannot be converted to a string.

However, if you write :

std::string test = "STRING";
test = test[0];

I would expect the same result, because, well, you're still trying to convert a char to a string. However, to my surprise, this actually compiles and works exactly the way you would expect (test == "S").

Could anyone explains this...?

๐ŸŒ
Google
google.com โ€บ goto
Converting char pointer to string - C++ Forum
March 5, 2013 - Secondly, the array you would point to would need to remain valid the whole time you were working with it, which in your case may not be possible without dynamic memory (which will get messy very quickly). Have you considered using std::vector? I highly recommend it - it manages lists of things for you and can dynamically resize by itself. In this case, "toks" would be std::vector<std::string>, and "array" would be std::vector<std::vector<std::string> >
๐ŸŒ
Unity
discussions.unity.com โ€บ unity engine
help convert char to string - Unity Engine - Unity Discussions
August 17, 2010 - Hi You can help me is possible convert char to string? tenks
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
Char array to String - Programming - Arduino Forum
June 15, 2018 - The noob is here again :slight_smile: I actually have two questions: Iโ€™m programming the ESP8266 and the ingenious Arduino libraries (Thx to Ivan and many others) seem to use strings intensively. Does the recommendatโ€ฆ
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ how-to-convert-std-string-to-char-in-cpp
How to Convert a std::string to char* in C++? - GeeksforGeeks
July 23, 2025 - To convert a std::string to a char*, we can use the std::string:c_str() method that returns a pointer to a null-terminated character array (a C-style string).
๐ŸŒ
Google
google.com โ€บ r/arduino โ€บ convert char to string
r/arduino on Reddit: Convert char to String
August 22, 2021 - Problem is i am only getting the last letter of the message, e.g when receiving OFF my string only has the last "F" when i get "ON" my String only has the "N": void callback(char* topic, byte* payload, unsigned int length) {
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-convert-char-to-string-in-java
How to Convert Char to String in Java? - GeeksforGeeks
July 23, 2025 - We can convert a char to a string object in Java by using java.lang.Character class, which is a wrapper for the char primitive type. Note: This method may arise a warning due to the new keyword as Character(char) in Character has been deprecated and marked for removal.
๐ŸŒ
Reddit
reddit.com โ€บ r/csharp โ€บ char array to text string?
r/csharp on Reddit: Char array to text string?
December 28, 2018 -

As a beginner with code, I'm trying to create a simple console Hangman game.

I think what I want to do is convert the user's word to a character array, which I do using:

char[] answerArray = word.ToCharArray();

Once it's into the array though, I can't get it to display in a single line again?

If I use:

Console.WriteLine(answerArray.ToString);

It shows every character as "System.Char[]".

The best I can figure out is:

Console.WriteLine("{0} {1} {2}..." , answerArray[0], answerArray[1], answerArray[2]...);

It seems that this has to be hardcoded this way though, and an undefined reference in Console.WriteLine causes an exception, which means I have to specify a word with a certain number of characters, otherwise the program will crash.

I've seen pages on stackoverflow and other sites saying to use a foreach loop but that is still giving me each letter on a separate line.

Thanks for any help!

๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ how do i turn std::string to char* ?
r/cpp_questions on Reddit: How do i turn std::string to char* ?
December 31, 2025 -

I need to compile shaders for OpenGL and I need to provide "shaderSource" for that, shaderSource must be char*, but I made a function that reads file contents into a variable, but that variable is an std::string, and I can't convert an std::strings to a char* with (char*), so I made this function

char* FileToChrP(const std::string& FileName) {
    std::ifstream file(FileName, std::ios::binary | std::ios::ate);
    if (!file.is_open()) {
        throw std::runtime_error("Your file is cooked twin | FileToChrP");
    }


    std::streamsize size = file.tellg();
    if (size < 0) throw std::runtime_error("Ur file is cooked twin | FileToChrP");
    file.seekg(0, std::ios::beg);


    char* buffer = new char[size + 1];


    file.read(buffer, size);
    buffer[size] = '\0';


    return buffer;
}char* FileToChrP(const std::string& FileName) {
    std::ifstream file(FileName, std::ios::binary | std::ios::ate);
    if (!file.is_open()) {
        throw std::runtime_error("Your file is cooked twin | FileToChrP");
    }


    std::streamsize size = file.tellg();
    if (size < 0) throw std::runtime_error("Ur file is cooked twin | FileToChrP");
    file.seekg(0, std::ios::beg);


    char* buffer = new char[size + 1];


    file.read(buffer, size);
    buffer[size] = '\0';


    return buffer;
}

but there's a problem, i have to manually delete the buffer with delete[] buffer and that feels wrong.
Also, this seems like a thing that c++ would already have. Is there abetter solution?