How to Convert a Char to a String

How to convert char[] to string in java?

You can use String.valueOf(char[]):

String.valueOf(c)

Under the hood, this calls the String(char[]) constructor. I always prefer factory-esque methods to constructors, but you could have used new String(c) just as easily, as several other answers have suggested.


char[] c = {'x', 'y', 'z'};
String s = String.valueOf(c);

System.out.println(s);
xyz

How to convert/parse from String to char in java?

If your string contains exactly one character the simplest way to convert it to a character is probably to call the charAt method:

char c = s.charAt(0);

C++ convert from 1 char to string?

All of

std::string s(1, c); std::cout << s << std::endl;

and

std::cout << std::string(1, c) << std::endl;

and

std::string s; s.push_back(c); std::cout << s << std::endl;

worked for me.

Is this the correct way to convert a char to a String in Java?

The shortest solution:

char c = 'a';
String s = "" + c;

The cleanest (and probably most efficient1) solutions:

char c = 'a';

String s1 = String.valueOf(c);
// or
String s2 = Character.toString(c);

Compared to your approach, the above prevents the overhead of having to box the primitive into a Character object.


1 The slight inefficiency of the string concatenation in the first approach might be optimized away by the compiler or runtime, so one (if one were so inclined) would really have to run a micro-benchmark to determine actual performance. Micro-optimizations like this are rarely worth the effort, and relying on concatenation to force string conversion just doesn't look very nice.

Java: Converting a char to a string

Easiest way?

String x = 'c'+"";

or of course

String.valueOf('c');

How do I convert a single char to a string?

Use the .ToString() Method

String myString = "Hello, World";
foreach (Char c in myString)
{
String cString = c.ToString();
}

How to convert character array (char[]) to String array (String[]) without looping through char array and by using predefined methods in java?

char[] charArray={'A','B','C'}; // Character array initialized
/**
*Below line will first convert a charArray to string using
*String(char[]) constructor and using String class method
*split(regularExpression) the converted string will
*then be splited with empty string literal delimiter which in turn
*returns String[]
**/
String[] result=new String(charArray).split("");

How to convert from char to string in Julia?

A string can already be seen as a collection of characters, so you shouldn't need to split the word.

However, map is specialized in such a way that on strings you can only map functions which return chars. And strings are also treated as scalars by the broadcasting system. This leaves us with a few options: a simple for loop or maybe a generator/comprehension.

I think in this case I'd go with the comprehension:

function char_to_one_hot(char, char_id, max_length)
one_hot = zeros(max_length)
setindex!(one_hot, 1.0, char_id[char])
end

function word_to_one_hot(word, char_id, max_length)
[char_to_one_hot(char, char_id, max_length) for char in word]
end

which I think gives what you'd expect:

julia> vocab = "abc"
"abc"

julia> char_id = Dict([ (index, char) for (char, index) in enumerate(vocab) ])
Dict{Char,Int64} with 3 entries:
'a' => 1
'c' => 3
'b' => 2

julia> word_to_one_hot("acb", char_id, 5)
3-element Array{Array{Float64,1},1}:
[1.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 1.0, 0.0, 0.0]
[0.0, 1.0, 0.0, 0.0, 0.0]

If you still want to convert between 1-character strings and characters, you can do it this way:

julia> str="a"; first(str)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> chr='a'; string(chr)
"a"


Related Topics



Leave a reply



Submit