我正在努力为我的问题找到一个明确的答案,它不是与网站上任何其他问题的重复.我在 SO 和其他几个网站上阅读了许多关于此的帖子和相关问题.例如这个是关键答案(许多其他被标记为重复并重定向到这个):C#中VB的Asc()和Chr()函数等价于什么?
I am trying to find a clear answer to my question and it is not a duplicate of any other questions on the site. I have read many posts and related questions on this on SO and several other sites. For example this one which is the key answer (many others are marked off as dulpicates and redirect to this one): What's the equivalent of VB's Asc() and Chr() functions in C#?
我正在将 VBA 宏转换为 C#.而在 VBA 中,chr(7)
可以简单地连接到 string
,就好像 chr()
会产生一个 string
.为什么这不能在 C# 中完成?
I was converting a VBA macro to C#. And in VBA chr(7)
can simply be concatenated to a string
as if chr()
would yield a string
. Why can't this be done in C#?
不幸的是,答案并不清楚,他们多次声明这是正确的用法:
And unfortunately the answer is not clear and many times they state that this is a correct use:
string mystring=(char)7;
但它给了我一个编译器错误,因为它没有作为字符串进行评估.
Yet it gives me a compiler error as it does not evaluate as a string.
我不得不使用它来使它工作:
I had to use this to make it work:
string mystring=((char)7).ToString();
这相当于 VB Chr() 函数,实际上就像 VB 中的 Chr() 计算为字符串一样.
This would be the equivalent of the VB Chr() function, really as Chr() in VB evaluates as a string.
我的问题是:我总是需要将 char
显式转换为 string
还是在某些情况下它会隐式转换?
My question is this: do I always need to cast the char
over to string
explicitly or there are some cases where it converts over implicitly?
更新:
根据@Dirk 的回答,这也有效:
Per @Dirk's answer, this also works:
string mystring = "" + (char)7;
这并没有减少我的神秘感.如果连接有效,为什么没有隐式转换??
This does not lessen my mystery. If concatenation works why there is no implicit cast??
我想获得关于 VB Chr() 与其在 C# 中的等价物之间的区别的完整解释.我将不胜感激任何我可以阅读的参考资料,甚至可以使用示例.提前致谢.
I would like to get a full explanation on the difference between the VB Chr() and its equivalents in C#. I would appreciate any reference where I can read up on, or even examples would do. Thanks in advance.
你带着这个问题打开了潘多拉魔盒.Chr() 是 VB.NET 中的遗留函数,任何现代代码都应该使用 ChrW() 代替.不同之处在于应该解释字符值的方式,ChrW() 假定字符代码是 Unicode(W = 宽).Chr() 将时钟回滚到上一个世纪,这是一个没有 Unicode 的石器时代,字符要么在 ASCII 字符集 (0..127) 中,要么在扩展"字符 (128..255) 中.其中扩展字符属于代码页.很多很多不同的代码页都是通用的.一场非常严重的灾难,程序无法正确解释由位于不同国家的另一台机器生成的文本.甚至在同一个国家,日本也有多个共同使用的代码页,但没有一个占主导地位.制作 mojibake.
You are opening Pandora's box with this question. Chr() is a legacy function in VB.NET, any modern code should be using ChrW() instead. The difference is the way the character value should be interpreted, ChrW() assumes the character code is Unicode (W = wide). Chr() rolls back the clock to the previous century, a stone age without Unicode where characters were either in the ASCII character set (0..127) or an "extended" character (128..255). Where the extended characters belong to a code page. Many, many different code pages were in common use. A very significant disaster, programs could not properly interpret text that was generated by another machine located in a different country. Or even in the same country, Japan had multiple code pages in common use with none of them dominant. Producing mojibake.
我假设你的意思是 ChrW(),没有人喜欢 mojibake.也不是 C#.使用 Char.ToString() 很好,替代方法是使用 字符串采用 char
的构造函数:
I'll assume you mean ChrW(), nobody likes mojibake. Not C# either. Using Char.ToString() is fine, the alternative is to use the string constructor that takes a char
:
string mystring = new string((char)7, 1);
或者您可能更喜欢的更一般的形式:
Or the more general form you might prefer:
public static string ChrW(int code) {
return new string((char)code, 1);
}
不是唯一的方法,使用文字也是可能的,并且可能是您更喜欢使用辅助方法的方法.以及 C# 不需要像 Chr() 这样的辅助函数的基本原因.ASCII 控制代码 7 是响铃字符,当您将其写入控制台时它会发出哔哔声,您可以使用转义符:
Not the only way to do it, using literals is possible as well and likely to be what you prefer over a helper method. And the basic reason that C# does not need a helper function like Chr(). ASCII control code 7 is the bell character, it BEEPs you when you write it to the console, you can use an escape for that:
string mystring = "a";
并不完全令人难忘,这来自 Unix.其他的是"代表退格, "代表制表符,
"代表回车,
"代表换行.Console.Write(" ");
是删除控制台窗口中最后输入字符的经典技巧.应注意 Environment.NewLine
属性.这大约是您应该使用控制字符推动它的程度.
Not exactly memorable, this comes from Unix. Other ones are "" for backspace, " " for a tab, "
" for a carriage return and "
" for a line feed. A classic trick to erase the last typed character in a console window is Console.Write(" ");
. The Environment.NewLine
property should be noted. Which is about as far as you should push it with control characters.
最后但并非最不重要的是 U 和 u 说明符,可让您对任何字符进行编码:
And last but not least the U and u specifier that lets you encode any character:
string mystring = "u0007";
从示例中看不出来,但是 u 值需要是十六进制的.当您使用来自高位 Unicode 位平面的代码点时,需要 U.
Not obvious from the example but the u value needs to be hexadecimal. U is needed when you use codepoints from the upper Unicode bit planes.
这篇关于来自 Int 的 C# Char 用作字符串 - VB Chr() 的真正等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!