0

I have to concatenate k different lengths of strings into one string res and save res string into ArrayBuffer[String]().
But the k is variable.
For example,

val result = new ArrayBuffer[String]()
result.+=("1\t" + A.toString() + "\t" + ls.pid + "\t" + ls.did + "\t" + ls.sid + "\t" + ls.request_time.substring(0,10))

result.+=("2\t" + B.toString() + "\t" + ls.pid + "\t" + ls.did + "\t" + ls.sid + "\t")

result.+=("2\t" + B.toString() + "\t" + ls.pid + "\t" + ls.did + "\t")

result.+=("2\t" + B.toString() + "\t")

How to use a function with a variable-length argument to implement it?

Thanks in advance.

Bowen Peng
  • 1,635
  • 4
  • 21
  • 39

1 Answers1

4

You can use the following syntax:

def f(args: String*) = {
    args.map{s =>
    //todo: process single item
    s
  }
}
Bondarenko
  • 225
  • 1
  • 7
  • 5
    I would just add the note that `String*` means is just a `Seq[Sring]` inside the method body so OP can do anything you can do on a sequence, like `map` or `foldLef` or `foreach`, etc. – Luis Miguel Mejía Suárez Aug 19 '21 at 13:17