“字符文字错误中字符过多”

发布于 2024-10-31 08:44:44 字数 260 浏览 2 评论 0原文

我正在努力处理一段代码并收到错误:

字符文字错误中字符过多

使用 C# 和 switch 语句迭代字符串缓冲区并读取标记,但在这一行中出现错误:

案例“&&”:

案例“||”:

案例“==”:

如何将 ==&& 保留为字符?

I'm struggling with a piece of code and getting the error:

Too many characters in character literal error

Using C# and switch statement to iterate through a string buffer and reading tokens, but getting the error in this line:

case '&&':

case '||':

case '==':

How can I keep the == and && as a char?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

始于初秋 2024-11-07 08:44:44

这是因为,在 C# 中,单引号 ('') 表示(或封装)单个字符,而双引号 ("") 用于表示字符串。例如:

var myChar = '=';

var myString = "==";

This is because, in C#, single quotes ('') denote (or encapsulate) a single character, whereas double quotes ("") are used for a string of characters. For example:

var myChar = '=';

var myString = "==";
半城柳色半声笛 2024-11-07 08:44:44

下面是一个示例:

char myChar = '|';
string myString = "||";

字符由单引号分隔,字符串由双引号分隔。

好消息是 C# switch 语句可以使用字符串!

switch (mytoken)
{
    case "==":
        //Something here.
        break;
    default:
        //Handle when no token is found.
        break;
}

Here's an example:

char myChar = '|';
string myString = "||";

Chars are delimited by single quotes, and strings by double quotes.

The good news is C# switch statements work with strings!

switch (mytoken)
{
    case "==":
        //Something here.
        break;
    default:
        //Handle when no token is found.
        break;
}
请帮我爱他 2024-11-07 08:44:44

您不能将 ==|| 视为字符,因为它们不是字符,而是字符序列。

你可以让你的 switch...case 在字符串上工作。

You cannot treat == or || as chars, since they are not chars, but a sequence of chars.

You could make your switch...case work on strings instead.

花落人断肠 2024-11-07 08:44:44

char 只能容纳单个字符,字符文字是单引号中的单个字符,即 '&' - 如果您的字符数多于您想要使用字符串的字符,为此您必须使用双引号:

case "&&": 

A char can hold a single character only, a character literal is a single character in single quote, i.e. '&' - if you have more characters than one you want to use a string, for that you have to use double quotes:

case "&&": 
面如桃花 2024-11-07 08:44:44

我遇到了同样的问题。
String.Replace('\\.','') 不是有效的语句,并会引发相同的错误。
感谢 C#,我们可以使用双引号代替单引号,并且可以进行以下工作
String.Replace("\\.","")

I faced the same issue.
String.Replace('\\.','') is not valid statement and throws the same error.
Thanks to C# we can use double quotes instead of single quotes and following works
String.Replace("\\.","")

淡淡離愁欲言轉身 2024-11-07 08:44:44

我相信您可以使用 Unicode 编码来做到这一点,但我怀疑这是否是您真正想要的。

== 是 unicode 值 2A76,所以我相信你可以这样做:

char c = '\u2A76';

我目前无法测试它,但我有兴趣知道这是否适合你。

您将需要四处挖掘其他人。如果您想查看的话,这里有一个 unicode 表:

http://www.tamasoft.co.jp/en/general-info/unicode.html" rel="nofollow">http://www. tamasoft.co.jp/en/general-info/unicode.html

I believe you can do this using a Unicode encoding, but I doubt this is what you really want.

The == is the unicode value 2A76 so I belive you can do this:

char c = '\u2A76';

I can't test this at the moment but I'd be interested to know if that works for you.

You will need to dig around for the others. Here is a unicode table if you want to look:

http://www.tamasoft.co.jp/en/general-info/unicode.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文