views:

63

answers:

5

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?

+1  A: 

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.

You're 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.

Hans Passant
\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
Oops, you're right. Post updated.
Hans Passant
+1: for telling reader about the *Character Map* utility (charmap.exe). Great tip that I use all the time.
AMissico
+1  A: 

Use the ChrW() function to return Unicode characters.

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

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

AMissico
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
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
A: 

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 
MarkJ
A: 

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