C#Char from Int用作String - VB的实际等价物Chr() [英] C# Char from Int used as String - the real equivalent of VB Chr()

查看:135
本文介绍了C#Char from Int用作String - VB的实际等价物Chr()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个VBA宏转换为C#。并且在VBA chr(7)可以简单地连接到 string ,如同 chr )会生成字符串。为什么不能在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#?

我想为我的问题找到一个清楚的答案。我已经阅读了很多的帖子和相关问题在这上和SO等几个网站。例如,这是一个关键的答案(许多其他标记为dulpicates并重定向到这一个):在C#中,VB的Asc()和Chr()函数的效果是什么?

I am trying to find a clear answer to my question. 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#?

不幸的是,答案不清楚,很多次他们说这是一个正确的用法:

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 转换为<$ c

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?

UPDATE:

每个@ 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.

推荐答案

您正在使用此问题打开Pandora的框。 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是响铃字符,它BEEPs当你写它到控制台,你可以使用一个转义:

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。其他的是\b表示退格,\t表示选项卡,\r表示回车符,\\\
表示换行符。删除控制台窗口中最后一个输入字符的典型技巧是 Console.Write(\b \b); 。应注意 Environment.NewLine 属性。

Not exactly memorable, this comes from Unix. Other ones are "\b" for backspace, "\t" for a tab, "\r" for a carriage return and "\n" for a line feed. A classic trick to erase the last typed character in a console window is Console.Write("\b \b");. 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.

这篇关于C#Char from Int用作String - VB的实际等价物Chr()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆