22

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?

schlebe
  • 2,315
  • 2
  • 27
  • 39
CodeFusionMobile
  • 14,272
  • 24
  • 96
  • 138

7 Answers7

31

Use the ChrW() function to return Unicode characters.

Dim strW As String
strW = ChrW(&H25B2) & "More text"
fivebob
  • 670
  • 5
  • 6
  • The only problem is that it breaks for logical characters that require more than one UTF-16 `Char`s. In C# there is [`"\Uxxxxxxxx"`](https://msdn.microsoft.com/en-us/data/aa664669(v=vs.85)) with the large `U` and 8 digits to represent those, and they are translated to several `Char`s at once. To handle these in VB you would have to look up what surrogate chars they consist of and encode those as `ChrW(c1) & ChrW(c2)`. – GSerg Jul 23 '19 at 07:33
15

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.

mbomb007
  • 3,077
  • 2
  • 30
  • 51
Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • \u0030 is a plain 0 - 0 has the value 48, which is equal to 0x30, which is the requested codepoint, as those escapes are written in hexadecimal. You were probably thinking of *decimal* 30, \u001e, which, according to http://www.unicode.org/charts/PDF/U0000.pdf, is a control character called Record Separator (or INFORMATION SEPARATOR TWO in Unicode). (It *does* exist as a codepoint, but it has no glyph as it's meant to be a non-printable character.) – Michael Madsen Jun 29 '10 at 21:19
  • 1
    +1: for telling reader about the *Character Map* utility (charmap.exe). Great tip that I use all the time. – AMissico Jun 29 '10 at 21:59
7

Replace Chr with Convert.ToChar:

str = Convert.ToChar(&H0030) & "More text"
John Rah
  • 1,270
  • 9
  • 10
3

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 ), only method 4, 5 and 6 are working !

  1. ChrW() function indicates an error at build
  2. Convert.ToChar() function crashes at runtime
  3. Alt+N is refused because it accepts only 4 digits

Example

lblCharacter.Text = "This solution works "
Debug.Print (Char.ConvertFromUtf32(&H1F600))
s = <text>diable: &#x1F608;</text>

PS: smiley pasted (0x1F600) directly in Visual Studio code editor or Notepad++ have lost background color ! Explanation: the smiley pasted in this answer is filled by orange color but in Visual Studio editor or Notepad++, this color has disappeared !


To use String literals in Visual Studio Editor, you must use method 3 or 4 !

In Form (Design mode)

enter image description here

In Properties (see Text property)

enter image description here

schlebe
  • 2,315
  • 2
  • 27
  • 39
  • I needed to send 32-bit codes for my application. The Char.ConvertFromUtf32(n) function worked perfectly for me. Thanks! – Bill Norman Nov 18 '20 at 19:01
2

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

Jaymin
  • 2,793
  • 3
  • 18
  • 33
AMissico
  • 20,847
  • 6
  • 69
  • 105
  • This won't solve the problem when the code file is written in a character set that doesn't support those characters - and could cause problems if your editor's font doesn't support all of the Unicode characters. Not a good solution! – Dan Puzey Jun 30 '10 at 12:07
  • 1
    But it *is* the best solution if the file is written in a Unicode encoding (as all modern text files should be) and if the font supports the characters. – Philipp Jun 30 '10 at 12:57
2

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 
Community
  • 1
  • 1
MarkJ
  • 29,252
  • 4
  • 60
  • 104
  • this work well in 2020 on Visual Studio 2019. See my answer later.I don't have any problem with compilation. Please test again and adapt your answer (I vote up) – schlebe Apr 20 '20 at 05:51
0

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.

Philipp
  • 43,805
  • 12
  • 78
  • 104
  • 3
    It's not that it's more typing it's that it can't be done in the form designer and its more tedious designing a form when you have to run it everytime you want to see it. – CodeFusionMobile Jun 30 '10 at 15:42