How to represent Unicode Chr Code in VB.Net String literal?

感情迁移 提交于 2019-11-27 15:28:06

Use the ChrW() function to return Unicode characters.

Dim strW As String
strW = ChrW(&H25B2) & "More text"
Hans Passant

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"
MarkJ

I was hoping you could use XML literals and XML escapes but it doesn't work. I don't think XML literals allow you to use &#NN;. Although it is a way of including quotes " inside strings.

'Does not compile :('
Dim myString = _ 
<q>This string would contain an escaped character &#30; if it actually compiled.</q>.Value 

I use the Character Map utility (charmap.exe). Run and select the characters you want in the control's font, such as ©Missico™, copy then paste into the Text property in the property grid. You will have to change the font because the default font for a form is "Microsoft Sans Serif" which is not a Unicode font. I do not think you can use this method for non-printable characters.

Depending on your needs, you can also use Localization, which creates resource files for each language. Again, you would use charmap.exe to select and copy the characters needed and paste into the resource file. You probably can use non-printable characters, such as tabs, newline, and so on, since this is just a text file (Unicode).

No, it's not possible since VB strings don't support escape sequences. Simply use ChrW, which is a few characters more to type, but also a bit cleaner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!