How to represent Unicode character in VB.Net String literal?
I know you can put Unicode character codes in a VB.Net string like this:
str = Chr(&H0030) & "More text"
I开发者_运维知识库 would like to know how I can put the char code right into the string literal so I can use Unicode symbols from the designer view.
Is this even possible?
Use the ChrW()
function to return Unicode characters.
Dim strW As String
strW = ChrW(&H25B2) & "More text"
The C# language supports this with escapes:
var str = "\u0030More text";
But that isn't available in VB.NET. Beware that you almost certainly don't want to use Chr(), that is meant for legacy code that works with the default code page. You'll want ChrW() and pass the Unicode codepoint.
Your specific example is not a problem, &H0030 is the code for "0" so you can simply put it directly in the string literal.
Dim str As String = "0MoreText"
You can use the Charmap.exe utility to copy and paste glyphs that don't have an easy ASCII code.
Replace Chr with Convert.ToChar:
str = Convert.ToChar(&H0030) & "More text"
To display an Unicode character, you can use following statement
ChrW(n)
wheren
is the number representing de Unicode character.Convert.ToChar(n)
- type directly character in editor using
Alt + N
key combination - paste/copy Unicode character directly in editor
Char.ConvertFromUtf32(n)
- XML String using
&#x....;
syntax
Example to assign ♥ character :
s = ChrW(&H2665)
s = Convert.ToChar(&H2665)
s = "♥" 'in typing Alt+2665
s = "♥" 'using paste/copy of ♥ from another location
s = Char.ConvertFromUtf32(&H2665)
s = <text>I ♥ you</text>
BUT when Unicode Character is greater than 0xFFFF (C syntax is more readable
精彩评论