How to represent Unicode character in VB.Net String literal? - vb.net

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
ChrW(n) where n is the number representing de Unicode character.
Convert.ToChar(n)
type directly character in editor using Alt + N key combination
paste/copy Unicode character directly in editor
Char.ConvertFromUtf32(n)
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 ♥ you</text>
BUT when Unicode Character is greater than 0xFFFF (C syntax is more readable 😉), only method 4, 5 and 6 are working !
ChrW() function indicates an error at build
Convert.ToChar() function crashes at runtime
Alt+N is refused because it accepts only 4 digits
Example
lblCharacter.Text = "This solution works 😉"
Debug.Print (Char.ConvertFromUtf32(&H1F600))
s = <text>diable: 😈</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)
In Properties (see Text property)

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

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.

Related

Typing Spanish characters into VBE

I have to type Spanish character (ñ) in VBE but I get n instead of ñ. I'm using polish version of Excel. Do you know how I can change that?
If you're typing the letter into a string then you can use the chr function. For example:
mystring = "jalepe" & chr(164) & "o"
It lengthens what should be simple strings, but it gets the work done.
If you need ñ for a function name or something similar, then hod's comment to your question should help you.

Read a text file and display result in a window

I have a text file which contains about 60 lines. I would like to parse out all the text from that file and display in a window. The text file contains words that are separated by an underscore. I would like to use regular expression to solve this problem.
Update:
This is my code as of now. I am trying to read "filename" in my code.
Dim filename = "D:\databases.txt"
Dim regexpression As String = "/^[^_]*_([^_]*)\w/"
I know I don't have much done here anyway but I am trying to learn VB on my own and have gotten stuck here.
Please feel free to suggest what I should be doing instead.
Something like this:
TextBox1.Lines = IO.File.ReadAllLines("fileName")
To remove underscores:
TextBox1.Lines = IO.File.ReadAllLines("fileName").Replace("_", String.Empty)
If you also need other special characters removed, you can use Regex.Replace:
Remove special characters from a string
Also on MSDN:
How to: Strip Invalid Characters from a String
Or the old school way - loop through all characters, and filter only those you need:
Most efficient way to remove special characters from string

Turkish Characters in the VBA editor

I have a problem with Turkish characters in my simple VBA code. Whenever I write some text in my module in Turkish characters (e.g. 'ş, ə, ç, ğ, ö, ü, ı"), they change to unknown letters.
I want to change "Eight" to "Səkkiz","Five", "Beş","Three" "Üç" etc.
May be you should change Font in the text editor
Try: Tools > Options at the 2nd tab choose Font Courier New (Turkish)
The VB editor doesn't support Unicode. Assuming that wherever you're displaying these characters does, you can do something like this:
Const UpsideDownE As Long = &H1DD
Sub Example()
' This would set the currently selected text in PowerPoint to ə
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange = ChrW(UpsideDownE)
End Sub
Of course, choose names for the constants that make sense to you ... whatever you'd normally call the ə character.

Replace a 'bel' character in a file in vb.NET

I've come across a character in one of my data feeds, which I have never encountered before
The images above are the data feed in Notepad++ and Notepad view. As you can see it appears as 'BEL' in Notepad++ and a sort of 'bullet point' in Notepad.
How would I go about replacing this character in vb.NET?
I've tried a simple replace in a SSIS Script Task by copying and pasting the character into the replace function, e.g.
text = text.Replace("copy and pasted character", "")
and this gives this error
All help is extremely appreciated,
Thanks
I’ve got no idea what SSIS is but since you wanted to know a solution in VB.NET, the code you’ve tried will work here. That is:
text = text.Replace("copy and pasted character", "")
will work just fine in VB. Alternatively, you can use the following:
text = text.Replace(Chr(7).ToString(), "")
Find out what the Ascii value of the character is and then use the Chr function to eliminate it
i.e.
text = text.Replace(Chr(n), "")
[Bell] is probably character 7

Displaying a Downward Triangle in VB.NET ▼ (U+25BC)

Hey, I'm trying to figure out how to display the ▼ character properly in a .NET winform application.
I am creating a custom control, and for the button, I want this character to appear. I am able to set the text to this character, but it appears as a blank square.
Any ideas on what I need to do to make this character appear properly on my forms?
I am using Arial font, which is compatible with this symbol.
EDIT: It is currently being set as follows:
btnCalendarToggle.Text = "▼" 'Yes, it appears exactly like this in my code
More information on the character can be found here:
http://www.fileformat.info/info/unicode/char/25bc/index.htm
EDIT2: I tried adding some other Unicode characters, and got the following message:
"Some Unicode Characters in this file
cannot be saved in the current
codepage. Do you want to resave this
file as Unicode in order to Maintain
your data?"
After clicking YES on this message, it still didn't work. It appears that the encoding method may be wrong for the file... I don't know what to set it to. Has anyone else tried to display this character in a winform before?
There can often be issues (both with source control systes and diff tools) if you embed more complex unicode characters in source files.
It is often better to do it via an explicit escape sequence and keep the source file in a simpler encoding.
btnCalendarToggle.Text = "\u25BC";
If this works it is likely that the problem is instead the encoding settings for the source file.
Are you certain however that the font in question is Arial (try debugging and checking) since regardless of the above mentioned issues so long as the encoding is set to a legitimate Unicode one (and Visual Studio will convert the file for you if you embed such a character in it) this should have worked.
Can you post the code you are currently using ?
You can print out characters using the chr(int) function if you know the character code.
Dim i As Integer
For i = 0 To 255
txtTest.Text = txtTest.Text & Chr(i) & " -- " & i.ToString() & Environment.NewLine
Next i
Try that and see if your character prints out.