带有开头、分隔符和结尾的 Scala List mkString() 方法示例
此方法与mkString()方法相同,但这里还包括一个开始、一个分隔符和一个结束。
Method Definition: defmkString(start: String, sep: String, end: String): String
Return Type: It returns the elements of the list as a string along with a stated start, separator and an end.
让我们来看看一些例子。
示例 #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/