我如何在 Scala 中表示一个空的 Char?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8306060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I represent an empty Char in Scala?
提问by deltanovember
val mychar=''
Does not compile and results in the following error:
不编译并导致以下错误:
error: unclosed character literal
错误:未闭合的字符文字
val mychar='a'
etc is fine. I've tried playing around with converting "" to char but without much luck
等很好。我试过将 "" 转换为字符,但运气不佳
回答by
There is no such thing as an "empty" Character -- what is an "empty" Integer? :)
没有“空”字符这样的东西——什么是“空”整数?:)
The closest is the NUL-character, which has an ordinal value of 0 ('\0', 0.toChar, 0: Char)...
最接近的是NUL 字符,它的序数值为 0 ( '\0', 0.toChar, 0: Char)...
...or perhaps Option[Char]would be able to better-describe the situation?
...或者也许Option[Char]能够更好地描述这种情况?
val someChar = Some('a')
val noChar = None: Option[Char]
val maybeChar = List(someChar, noChar)((Math.random * 2).toInt)
// ...
Happy coding.
快乐编码。
回答by Abdelwahed Houbouby
im was trying same thing i had to return empty char or something like thats '' so what i did when val c : char = '\0'
我正在尝试同样的事情,我不得不返回空字符或类似的东西 '' 所以我在 val c 时做了什么:char = '\0'
val c : char = " ".charAt(0)

