带有分隔符的 Scala 设置 mkString() 方法和示例
mkString()方法用于在字符串中显示集合的所有元素以及分隔符。
Method Definition: def mkString(sep: String): String
Return Type: It returns all the elements of the set in a string along with a separator.
示例 #1:
// Scala program of mkString()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(1, 2, 3, 4)
// Applying mkString method
val result = s1.mkString(", ")
// Display output
println(result)
}
}
输出:
1, 2, 3, 4
示例 #2:
// Scala program of mkString()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set("Geeks", "Will", "Rule")
// Applying mkString method
val result = s1.mkString(" ")
// Display output
println(result)
}
}
输出:
Geeks Will Rule