scala 将字符转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15589978/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:05:21  来源:igfitidea点击:

Convert Char into a String

scala

提问by Aslan986

Given s : Stringhow can I cast the result of

鉴于s : String我如何才能投射结果

s.first()

into a String?

String?

采纳答案by Ryan

String doesn't have a .first()function. Do you mean .head?

字符串没有.first()函数。你的意思是.head

Using headand returning a String is as simple as:

使用head和返回 String 非常简单:

s.head.toString

回答by Eastsun

You can use the method takeas followed:

您可以使用以下方法take

scala> val s = "abcdef"
s: String = abcdef

scala> val first = s.take(1)
first: String = a

scala> 

回答by Ignacio Alorre

Another option:

另外一个选择:

val s : String = "hello"
val first : String = s(0)+""