scala 字符串连接不再起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9379014/
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
String concatenation gone functional
提问by Hyman
Suppose there are 3 strings:
假设有 3 个字符串:
protein, starch, drink
Concatenating those, we could say what is for dinner:
将这些连接起来,我们可以说晚餐吃什么:
Example:
例子:
val protein = "fish"
val starch = "chips"
val drink = "wine"
val dinner = protein + ", " + starch + ", " + drink
But what if something was missing, for example the protein, because my wife couldn't catch anything. Then, we will have: ,chips, drinkfor dinner.
但是如果缺少某些东西怎么办,例如蛋白质,因为我妻子什么也抓不到。然后,我们将有:,chips, drink晚餐。
There is a slick way to concatenate the strings to optionally add the commas - I just don't know what it is ;-). Does anyone have a nice idea?
有一种巧妙的方法可以连接字符串以选择性地添加逗号 -我只是不知道它是什么 ;-)。有人有什么好主意吗?
I'm looking for something like:
我正在寻找类似的东西:
val dinner = protein +[add a comma if protein is not lenth of zero] + starch .....
It's just a fun exercise I'm doing, so now sweat if it can't be done in some cool way. The reason that I'm trying to do the conditional concatenation in a single assignment, is because I'm using this type of thing a lot in XML and a nice solution will make things..... nicer.
这只是我正在做的一个有趣的练习,所以现在如果不能以某种很酷的方式完成,那就出汗吧。我尝试在单个赋值中进行条件连接的原因是因为我在 XML 中经常使用这种类型的东西,一个好的解决方案将使事情......更好。
回答by Alexander Azarov
When you say "it may be absent", this entity's type should be Option[T]. Then,
当你说“它可能不存在”时,这个实体的类型应该是Option[T]. 然后,
def dinner(components: List[Option[String]]) = components.flatten mkString ", "
You would invoke it like this:
你会像这样调用它:
scala> dinner(None :: Some("chips") :: Some("wine") :: Nil)
res0: String = chips, wine
In case you absolutely want checking a string's emptiness,
如果您绝对想检查字符串的空性,
def dinner(strings: List[String]) = strings filter {_.nonEmpty} mkString ", "
scala> dinner("" :: "chips" :: "wine" :: Nil)
res1: String = chips, wine
回答by user unknown
You're looking for mkString on collections, maybe.
您可能正在寻找集合上的 mkString。
val protein = "fish"
val starch = "chips"
val drink = "wine"
val complete = List (protein, starch, drink)
val partly = List (protein, starch)
complete.mkString (", ")
partly.mkString (", ")
results in:
结果是:
res47: String = fish, chips, wine
res48: String = fish, chips
You may even specify a start and end:
您甚至可以指定开始和结束:
scala> partly.mkString ("<<", ", ", ">>")
res49: String = <<fish, chips>>
回答by Daniel C. Sobral
scala> def concat(ss: String*) = ss filter (_.nonEmpty) mkString ", "
concat: (ss: String*)String
scala> concat("fish", "chips", "wine")
res0: String = fish, chips, wine
scala> concat("", "chips", "wine")
res1: String = chips, wine
scala>
回答by Jesse
This takes care of the case of empty strings and also shows how you could put other logic for filtering and formatting. This will work fine for a List[String]and generalizes to List[Any].
这会处理空字符串的情况,还展示了如何放置其他逻辑来进行过滤和格式化。这将适用于 aList[String]并推广到List[Any].
val input = List("fish", "", "chips", 137, 32, 32.0, None, "wine")
val output = input.flatMap{ _ match {
case None => None
case x:String if !x.nonEmpty => None
case x:String => Some(x)
case _ => None
}}
.mkString(",")
res1: String = fish,chips,wine
The idea is that flatMaptakes a List[Any]and uses matching to assign Nonefor any elements that you do not want to keep in the output. The Nones get flattened away and the Somes stay.
这个想法是flatMap采用 aList[Any]并使用匹配来分配None您不想保留在输出中的任何元素。Nones 被夷为平地,而 Somes 留下来。
If you needed to be able to handle different types (Int, Double, etc) then you could add more cases.
如果您需要能够处理不同的类型(Int、Double 等),那么您可以添加更多案例。
回答by Deepak Saxena
println(s"$protein,$starch,$drink")

