开发者

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

  1. ChrW(n) where n is the number representing de Unicode character.
  2. Convert.ToChar(n)
  3. type directly character in editor using Alt + N key combination
  4. paste/copy Unicode character directly in editor
  5. Char.ConvertFromUtf32(n)
  6. 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 &#x2665; you</text>

BUT when Unicode Character is greater than 0xFFFF (C syntax is more readable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜