带有分隔符的 Scala List mkString() 方法和示例
mkString()方法用于在字符串中显示列表的所有元素以及分隔符。
Method Definition: def mkString(sep: String): String
Return Type: It returns all the elements of the list 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 list
val m1 = List(2, 3, 5, 7, 8)
// Applying mkString method
val result = m1.mkString("*")
// Displays output
println(result)
}
}
输出:
2*3*5*7*8
示例:2#
// Scala program of mkString()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(2, 3, 5, 7, 5)
// Applying mkString method
val result = m1.mkString("_")
// Displays output
println(result)
}
}
输出:
2_3_5_7_5