Tim*_*lds 34
在C#中的Unicode字符的文字\uXXXX,其中X的是十六进制字符将让您指定的Unicode字符.例如:
\u00A3 是英镑标志,£.\u20AC 是欧元符号,€.\u00A9 是版权符号,©.您可以像使用字符串中的任何其他字符一样使用这些Unicode字符文字.
例如,"15 \u00A3 per item"字符串"15£per item".
您可以将这样的字符串放在文本框中,就像使用任何其他字符串一样.
注意:您也可以只删除(Ctrl+ C)网站上的符号,例如维基百科(英镑符号),然后将其直接粘贴(Ctrl+ V)到C#源代码文件中的字符串文字中.C#源代码文件本身使用Unicode.这种方法完全可以让您不必知道所需符号的四个十六进制数字.
要与上面的示例并行,您可以简单地创建相同的字符串文字"15 £ per item".
编辑:如果要从其十六进制字符串动态创建Unicode字符,可以使用以下命令:
public static char HexToChar(string hex)
{
return (char)ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
Run Code Online (Sandbox Code Playgroud)
例如,HexToChar("20AC")会得到欧元符号.
如果要动态执行相反的操作:
public static string CharToHex(char c)
{
return ((ushort)c).ToString("X4");
}
Run Code Online (Sandbox Code Playgroud)
比如CharToHex('€')会得到你"20AC".
的选择ushort对应于可能的范围内char的值,示出在这里.
我不敢相信这在互联网上很难找到!
对于未来的开发人员,如果你有unicode字符,那么很容易做到.例如:
C#:
var selectionIndex = txt.SelectionStart;
string copyrightUnicode = "00A9";
int value = int.Parse(copyrightUnicode, System.Globalization.NumberStyles.HexNumber);
string symbol = char.ConvertFromUtf32(value).ToString();
txt.Text = txt.Text.Insert(selectionIndex, symbol);
txt.SelectionStart = selectionIndex + symbol.Length;
Run Code Online (Sandbox Code Playgroud)
VB.Net
Dim selectionIndex = txt.SelectionStart
Dim copyrightUnicode As String = "00A9"
Dim value As Integer = Integer.Parse(copyrightUnicode, System.Globalization.NumberStyles.HexNumber)
Dim symbol As String = Char.ConvertFromUtf32(value).ToString()
txt.Text = txt.Text.Insert(selectionIndex, symbol)
txt.SelectionStart = selectionIndex + symbol.Length
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57635 次 |
| 最近记录: |