how can I convert a char to a char* in c#?
I'm initializeing a String object like this:
String test=new String('c');
and I'm getting this error:
Argument '1': cannot convert from 'char' to 'char*'
That is a bit of a strange way to initialize a string, if you know beforehand what you want to store in it.
You can simply use:
String test="c";
If you have a specific need to convert a char variable to a string, you can use the built in ToString()
function:
String test = myCharVariable.ToString();
unsafe
{
char c = 'c';
char *ch = &c;
}
Your example has a String
and a compile error from using one of the String constructor overloads, so I'm guessing you really just want an array of chars, aka a String
and maybe not a char*
.
In which case:
char c = 'c';
string s = c.ToString(); // or...
string s1 = "" +c;
Also available:
unsafe
{
char c = 'c';
char* ch = &c;
string s1 = new string(ch);
string s2 = new string(c, 0);
}
string myString1 = new string(new char[] {'a'});
string myString2 = 'a'.ToString();
string myString3 = "a";
string myString4 = new string('a', 1);
unsafe {
char a = 'a';
string myString5 = new string(&a);
}
There is no overload of the public constructor for String
that accepts a single char
as a parameter. The closest match is
public String(char c, int count)
which creates a new String
that repeats the char c count
times. Thus, you could say
string s = new string('c', 1);
There are other options. There is a public constructor of String
that accepts a char[]
as a parameter:
public String(char[] value)
This will create a String
that is initialized with the Unicode characters in value
. Thus you could say
char c = 'c';
string s = new String(new char[] { c });
Another option is to say
char c = 'c'
string s = c.ToString();
But the most straightforward approach that most will expect to see is
string s = "c";
As for converting a char
to a char *
you can not safely do this. If you want to use the overload of the public constructor for String
that accepts a char *
as a parameter, you could do this:
unsafe {
char c = 'c';
char *p = &c;
string s = new string(p);
}
不能再有另一个答案:
string test = string.Empty + 'c';
String
类有许多构造函数,如果您要创建包含一个字符的字符串,则可以使用以下内容:
String test = new String(new char[] { 'c' });
如果你正在编写它,你有什么理由不能使用:
String test = "c";
怎么样:
var test = 'c'.ToString()
When using a char in the String constructor, you should also give a count parameter to specify how many times that character should be added to the string:
String test=new String('c', 1);
See also here .
使用
String test("Something");
String test = new String(new char [] {'c'});
The easiest way to do this conversion from your example is just change the type of quotes you are using from single quotes
String test = new String('c');
to double quotes and remove the constructor call:
String test = "c";
char c = 'R';
char *pc = &c;
Using single quotes (as in your question: 'c'
) means that you are creating a char
. Using double quotes, eg "c"
, means you creating a string
. These are not interchangable types in c#.
A char*
, as you might be aware, is how strings are represented in c++ to some extent, and c# supports some of the conventions of c++. This means that a char*
can easily (for the programmer at least) be converted to a string
in c#. Unfortunately a char is not inherently a char*, so the same cannot be done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.