EN
C# / .NET - insert unicode character to string
0 points
In this article, we would like to show you how to insert unicode character to string in C#.
There are three different ways how to do it:
- with escape character e.g.
\u03c0
that is equal toπ
, - by pasting character directly to string,
- by converting codes to a string.
Quick solution:
xxxxxxxxxx
1
string text = "π or \u03c0";
2
3
Console.WriteLine(text);
Output:
xxxxxxxxxx
1
π or π
Space is represented by 0x20
(in hex) or 32
(in dec). It is necessary to add 2 additional zeros before code (e.g. \u0020
).
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string text = "π and \u03c0 are\u0020equal"; // U+03C0 U+0020
8
9
Console.WriteLine(text);
10
}
11
}
Output:
xxxxxxxxxx
1
π and π are equal
In this section you can find some emojis that we pasted directly to the string and received e.g \uD83C\uDF4F
for the 🍏 (green apple emoji).
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string apple = "\uD83C\uDF4F";
8
string banana = "\uD83C\uDF4C";
9
string cherry = "\uD83C\uDF52";
10
11
Console.WriteLine(apple);
12
Console.WriteLine(banana);
13
Console.WriteLine(cherry);
14
}
15
}
Output:
xxxxxxxxxx
1
🍏
2
🍌
3
🍒