meital
meital

Reputation: 61

ListBuffer to assign it to one string

I have created loop on ListBuffer:

val listOfNamesOfMissingFiles = new ListBuffer[String]()
controlFileContent.entries.foreach { entry =>
      val fileName = entry("File_Name")
      val sha256 = entry("File_SHA_256")
      val file = new File(folder, fileName)
      if (!file.exists()) {
        logger.error(s"Control file validation failed: file does not exist in folder: [$fileName].")
        listOfNamesOfMissingFiles += fileName
      }

I want to assign what i am getting in listOfNamesOfMissingFiles to one string to an attribut which i am sending it as event to kibana portal. If my listOfNamesOfMissingFiles == ListBuffer("hhhh","uuuu"). if i will do listOfNamesOfMissingFiles.toString I will get "ListBuffer("hhhh","uuuu")". how can i remove the ListBuffer from the output?

Upvotes: 0

Views: 799

Answers (1)

Aki
Aki

Reputation: 1754

I think what you're trying to achieve is just joining a list of strings. Scala provides the mkString method:

scala> val names = ListBuffer("hhhh", "uuuu")
names: scala.collection.mutable.ListBuffer[String] = ListBuffer(hhhh, uuuu)

scala> names.mkString
res0: String = hhhhuuuu

scala> names.mkString(", ")
res1: String = hhhh, uuuu

scala> names.mkString("(", ", ", ")")
res2: String = (hhhh, uuuu)

Upvotes: 6

Related Questions