string 如何在 VB.Net 字符串文字中表示 Unicode 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3144053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:47:32  来源:igfitidea点击:

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

vb.netstringunicodedesigner

提问by CodeFusionMobile

I know you can put Unicode character codes in a VB.Net string like this:

我知道您可以将 Unicode 字符代码放入 VB.Net 字符串中,如下所示:

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.

我想知道如何将字符代码直接放入字符串文字中,以便我可以从设计器视图中使用 Unicode 符号。

Is this even possible?

这甚至可能吗?

回答by fivebob

Use the ChrW()function to return Unicode characters.

使用该ChrW()函数返回 Unicode 字符。

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

回答by Hans Passant

The C# language supports this with escapes:

C# 语言通过转义支持这一点:

var str = "\u0030More text";

But that isn't available in VB.NET. Beware that you almost certainly don'twant 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.

但这在 VB.NET 中不可用。要注意的是,你几乎肯定希望使用CHR(),这是为遗留代码与默认代码页的作品。您将需要 ChrW() 并传递 Unicode 代码点。

Your specific example is not a problem, &H0030 is the code for "0" so you can simply put it directly in the string literal.

您的具体示例没有问题,&H0030 是“0”的代码,因此您可以简单地将其直接放入字符串文字中。

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.

您可以使用 Charmap.exe 实用程序来复制和粘贴没有简单 ASCII 代码的字形。

回答by John Rah

Replace Chr with Convert.ToChar:

用 Convert.ToChar 替换 Chr:

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

回答by AMissico

I use the Character Maputility (charmap.exe). Run and select the characters you want in the control's font, such as ?Missico?, copy then paste into the Textproperty 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.

我使用字符映射实用程序 (charmap.exe)。运行并选择控件字体中所需的字符,例如?Missico?,复制然后粘贴到Text属性网格中的属性中。您必须更改字体,因为表单的默认字体是“Microsoft Sans Serif”,它不是 Unicode 字体。我认为您不能将这种方法用于不可打印的字符。

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

根据您的需要,您还可以使用 Localization,它为每种语言创建资源文件。同样,您将使用charmap.exe 选择并复制所需的字符并将它们粘贴到资源文件中。您可能可以使用不可打印的字符,例如制表符、换行符等,因为这只是一个文本文件 (Unicode)。

回答by 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 wayof including quotes "inside strings.

我希望你可以使用 XML 文字和 XML 转义,但它不起作用。我认为 XML 文字不允许您使用&#NN;. 虽然它是一种"在字符串中包含引号的方法

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

回答by Philipp

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.

不,这是不可能的,因为 VB 字符串不支持转义序列。只需使用ChrW,这会多输入几个字符,但也更简洁一些。

回答by schlebe

To display an Unicode character, you can use following statement

要显示 Unicode 字符,可以使用以下语句

  1. ChrW(n)where nis the number representing de Unicode character.
  2. Convert.ToChar(n)
  3. type directly character in editor using Alt + Nkey combination
  4. paste/copy Unicode character directly in editor
  5. Char.ConvertFromUtf32(n)
  6. XML String using &#x....;syntax
  1. ChrW(n)其中n是代表 de Unicode 字符的数字。
  2. Convert.ToChar(n)
  3. 使用Alt + N组合键在编辑器中直接输入字符
  4. 直接在编辑器中粘贴/复制 Unicode 字符
  5. Char.ConvertFromUtf32(n)
  6. XML 字符串使用&#x....;语法

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>

BUTwhen Unicode Character is greater than 0xFFFF(C syntax is more readable ), only method 4, 5 and 6 are working !

但是当 Unicode 字符大于0xFFFF(C 语法更易读)时,只有方法 4、5 和 6 有效!

  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
  1. ChrW()函数指示构建错误
  2. Convert.ToChar()函数在运行时崩溃
  3. Alt+N 被拒绝,因为它只接受 4 位数字

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 !

PS:直接在 Visual Studio 代码编辑器或 Notepad++ 中粘贴的笑脸 (0x1F600) 失去了背景颜色!说明:此答案中粘贴的笑脸由橙色填充,但在 Visual Studio 编辑器或 Notepad++ 中,此颜色已消失!



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

要在 Visual Studio 编辑器中使用字符串文字,您必须使用方法 3 或 4 !

In Form (Design mode)

在表单中(设计模式)

enter image description here

在此处输入图片说明

In Properties (see Text property)

在属性中(请参阅文本属性)

enter image description here

在此处输入图片说明