EN
TypeScript - insert unicode character to string
0 points
In this article, we are going to look at how to insert Unicode characters to string in TypeScript.
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
const text: string = 'π or \u03c0'; // U+03C0
2
3
console.log(text); // π or π
Note: it is important to put 4 digits after
\u
.
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
const text: string = 'π and \u03c0 are\u0020equal'; // U+03C0 U+0020
2
3
console.log(text); // π and π are equal
Marked inserted Unicode characters: π
and \u03c0
are \u0020
equal.