Is string concatenation in scala as costly as it is in Java?

前端 未结 4 662
陌清茗
陌清茗 2020-12-29 18:17

In Java, it\'s a common best practice to do string concatenation with StringBuilder due to the poor performance of appending strings using the + operator. Is the same practi

4条回答
  •  有刺的猬
    2020-12-29 18:55

    I want to add: if you have a sequence of strings, then there is already a method to create a new string out of them (all items, concatenated). It's called mkString.

    Example: (http://ideone.com/QJhkAG)

    val example = Seq("11111", "2222", "333", "444444")
    val result = example.mkString
    println(result) // prints "111112222333444444"
    

提交回复
热议问题