Scala当中mkString()方法的使用

mkString()方法的使用:

马克-to-win @ 马克java社区:防盗版实名手机尾号:73203

package com
object Test
{
  def main(args: Array[String]): Unit = {
  var name : String = "Hello mark-to-win"
  var tmp=""
/*def mkString(sep: String): String
Displays all elements of this string in a string using a separator string.
 */
  tmp=name.mkString(" ")
  println("name.mkString(\" \") is "+tmp)
  tmp=name.mkString(",")
  println("name.mkString(\",\") is "+tmp)
/*def mkString(start: String, sep: String, end: String): String
Displays all elements of this string in a string using start, end, and separator strings.
 * */
  tmp=name.mkString("begin",",","end")
  println("name.mkString(\"begin\",\",\",\"end\") is "+tmp)
  val aList = List(1,4,3,7,5)
/*def mkString(sep: String): String
Displays all elements of this list in a string using a separator string.
*/
  tmp=aList.mkString(",")
  println(tmp)
  }
}




输出的结果:

name.mkString(" ") is H e l l o   m a r k - t o - w i n
name.mkString(",") is H,e,l,l,o, ,m,a,r,k,-,t,o,-,w,i,n
name.mkString("begin",",","end") is beginH,e,l,l,o, ,m,a,r,k,-,t,o,-,w,i,nend
1,4,3,7,5