当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala Iterator mkString()用法及代码示例


此方法也与mkString()方法相同,但此处带有起点,分隔符和终点。

函数定义: def mkString(start:String, sep:String, end:String):String

Where, start is the starting string, sep is the separator string and end is the ending string.



返回类型: It returns the string representation of the stated collection along with a start, a separator and an end.

范例1:

// Scala program of mkString() 
// method with a start, end  
// and a separator 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating an Iterator  
        val iter = Iterator(1, 2, 3, 5) 
          
        // Applying mkString method  
        val result = iter.mkString("*", "0", "*") 
          
        // Displays output 
        println(result) 
      
    } 
}
输出:
*1020305*

其中,开始是*,sep是0,结束也是*。范例2:

// Scala program of mkString() 
// method with a start, end  
// and a separator 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating an Iterator  
        val iter = Iterator(1, 2, 3, 5) 
          
        // Applying mkString method  
        val result = iter.mkString("#", "*", "#") 
          
        // Displays output 
        println(result) 
          
    } 
}
输出:
#1*2*3*5#


相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Scala Iterator mkString() method with a start, a separator and an end with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。