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



Question:

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?


Solution:1

Use the ChrW() function to return Unicode characters.

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


Solution:2

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.


Solution:3

Replace Chr with Convert.ToChar:

str = Convert.ToChar(&H0030) & "More text"  


Solution:4

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   


Solution:5

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).


Solution:6

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »