简体   繁体   English

从元组读取并在Scala中制作字符串

[英]Reading from a Tuple and making a String in Scala

I have a list of Tuple(s) like: 我有一个类似的元组列表:

val rez = List((A, B, C, D, E, WrappedArray(F1, F2, F3)), (A2, B2, C2, D2, E2, WrappedArray(F4, F5)))

What I am trying to do is to create a string from each tuple in the list, so that (eg, for the first tuple): 我想做的是从列表中的每个元组创建一个字符串,以便(例如,对于第一个元组):

if(C == "Bob") then "A => (F1 \/ F2 \/ F3)"

The elements in the Tuple are, of course, of different types (in the WrappedArray() are of the same type). 当然,元组中的元素是不同类型的(在WrappedArray()中是相同类型的)。

My difficult part is how to make the string like (F1 \\/ F2 \\/ F3) from the elements within the WrappedArray() ? 我最困难的部分是如何从WrappedArray()的元素制作像(F1 \\/ F2 \\/ F3)这样的字符串?

Thanks for any help. 谢谢你的帮助。

If I understand your requirement correctly.. the below should work.. 如果我正确理解了您的要求,那么以下方法应该可以工作。

val rez = List(("A", "B", "Bob", "D", "E", Array("F1", "F2", "F3")), ("A2", "B2", "Bob", "D2", "E2", Array("F4", "F5")))

val result = rez map {
 case (a, b, c, d, e, array) if(c == "Bob") => s"$a => ${array.mkString(" \\/ ")}"
}
result: List[String] = List(A => F1 \/ F2 \/ F3, A2 => F4 \/ F5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM