带有分隔符的 Scala Iterator addString() 方法和示例
此方法与addString()方法相同,但在这里我们甚至可以在 Iterator 的所有元素之间包含分隔符。
Method Definition : def addString(b: StringBuilder, sep: String): StringBuilder
Where, sep is the separator.
Return Type :It returns the elements of an iterator in the String Builder along with the separator stated.
示例 #1:
// Scala program of addString()
// method along with separator
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(11, 12, 13, 14)
// Applying addString method
// with a separator
val result = iter.addString(new StringBuilder(), "#")
// Displays output
println(result)
}
}
输出:
11#12#13#14
示例 #2:
// Scala program of addString()
// method along with separator
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating an Iterator
val iter = Iterator(5, 6, 9)
// Applying addString method
// with a separator
val result = iter.addString(new StringBuilder(), "*")
// Displays output
println(result)
}
}
输出:
5*6*9