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)
surrounded.insert(0,ss)
surrounded.append(ss)
surrounded.mkString
}
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.