-
From the documentation, you can get a character from a int character = 'a';
string() s = $"Current character: {character:c}"; // Maybe something like this? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Excellent question!
Currently there's no format string for Unicode characters (or code units). I was about to add "C" as in your example. One problem is that in .NET the "C" format means "currency" which could be confusing. Another problem is that C (and probably C++ too, I'm not sure) have no standard library support for encoding UTF-8. As a workaround, you can encode your character in UTF-8, then use |
Beta Was this translation helpful? Give feedback.
-
public static class Debug
{
static string() CharToString(int c)
{
byte[4] bytes;
if (c <= 0x7f) {
bytes[0] = c;
return Encoding.UTF8.GetString(bytes, 0, 1);
}
if (c <= 0x7ff) {
bytes[0] = 0xc0 | c >> 6;
bytes[1] = 0x80 | (c & 0x3f);
return Encoding.UTF8.GetString(bytes, 0, 2);
}
if (c <= 0xffff) {
bytes[0] = 0xe0 | c >> 12;
bytes[1] = 0x80 | (c >> 6 & 0x3f);
bytes[2] = 0x80 | (c & 0x3f);
return Encoding.UTF8.GetString(bytes, 0, 3);
}
bytes[0] = 0xf0 | c >> 18;
bytes[1] = 0x80 | (c >> 12 & 0x3f);
bytes[2] = 0x80 | (c >> 6 & 0x3f);
bytes[3] = 0x80 | (c & 0x3f);
return Encoding.UTF8.GetString(bytes, 0, 4);
}
public static string() GetMessage(string str) => $"First character: {CharToString(str[0])}";
public static void Run() {
Console.WriteLine(GetMessage("Hi"));
}
} or: public static class Debug
{
public static string() GetMessage(string str)
{
StringWriter() writer;
writer.Write("First character: ");
writer.WriteChar(str[0]);
return writer.ToString();
}
public static void Run() {
Console.WriteLine(GetMessage("Hi"));
}
} but the built-in It's better to use this: public static class Debug
{
public static string() GetMessage(string str) => $"First character: {str.Substring(0, 1)}";
public static void Run() {
Console.WriteLine(GetMessage("Hi"));
}
} |
Beta Was this translation helpful? Give feedback.
-
That's correct, except it should be: public static string() GetMessage(string str)
{
string() out = "First character: ";
// Here goes some parsing steps...
out = $"{out}{str.Substring(0, 1)}";
// Other steps...
out = $"{out}\nSecond character: {str.Substring(1, 1)}";
return out;
} Note the use of Alternatively: out += str.Substring(0, 1); etc. Also, the above is only fine if you work on ASCII characters. For Unicode, the concept of "character" is more complex and please make sure you understand the UTF-8 and UTF-16 encodings. |
Beta Was this translation helpful? Give feedback.
-
Errors added in 4a7a5a4 |
Beta Was this translation helpful? Give feedback.
That's correct, except it should be:
Note the use of
string()
for string storage. This is crucial if you target C or C++. I'll try to add errors that prevent the incorrect usage of string references.Alternatively:
etc.
Also, the above is only fine if you work on ASCII characters. For Unicode, the concept of "character" is more complex and please make su…