Surrounding Scala Strings

If you do something in a single statement like "abc" + stringval + "abc", is that one immutable string copy, or two (noting that abc and 123 are constant at compile time)

Bonus round: would using a StringBuilder like the following have more or less overhead?

  def surround(s:String, ss:String):String = {
    val surrounded = new StringBuilder(s.length() + 2*ss.length(), s)

Or is there a more idiomatic way that I'm unaware of?


It has less overhead than concatenation. But the insert in your example is not efficient. The following is a little cleaner and uses only appends for efficiency.

def surround(s:String, ss:String) =
  new StringBuilder(s.length() + 2*ss.length(), ss).append(s).append(ss).mkString

Need Your Help

Synchronize Dataset - Multiple users, multiple timestamps

android ios dataset timestamp pandas

I'm trying to understand what is the best way to go about doing this:

Bind checkbox on DB initial null value

c# checkbox bind

I need to bind a checkBox but with this line of code,