“字符文字错误中字符过多”
我正在努力处理一段代码并收到错误:
字符文字错误中字符过多
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是因为,在 C# 中,单引号 (
''
) 表示(或封装)单个字符,而双引号 (""
) 用于表示字符串。例如: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:下面是一个示例:
字符由单引号分隔,字符串由双引号分隔。
好消息是 C# switch 语句可以使用字符串!
Here's an example:
Chars are delimited by single quotes, and strings by double quotes.
The good news is C# switch statements work with strings!
您不能将
==
或||
视为字符,因为它们不是字符,而是字符序列。你可以让你的 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.
char 只能容纳单个字符,字符文字是单引号中的单个字符,即
'&'
- 如果您的字符数多于您想要使用字符串的字符,为此您必须使用双引号: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:我遇到了同样的问题。
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("\\.","")
我相信您可以使用 Unicode 编码来做到这一点,但我怀疑这是否是您真正想要的。
==
是 unicode 值 2A76,所以我相信你可以这样做:我目前无法测试它,但我有兴趣知道这是否适合你。
您将需要四处挖掘其他人。如果您想查看的话,这里有一个 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: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