Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there ever a reason to use Scala's StringLike.replaceAllLiterally over Java's String.replace?

Tags:

java

scala

Scala's StringLike has the method replaceAllLiterally(literal: String, replacement: String): String

This seems to be very similar in effect to Java's String with the method String replace(CharSequence target, CharSequence replacement).

Is there ever a reason to use the Scala version?

(Not going through the regex compile step would seem to enable Java's version to be faster, though I haven't benchmarked that)

like image 403
Jxtps Avatar asked Oct 29 '13 17:10

Jxtps


1 Answers

It's to avoid collision with replace on StringBuilder. StringBuilder is also a StringLike. Why the StringBuilder replace wasn't the one to be changed, I'm not sure.

There's no reason to use it on strings unless you want to handle any StringLike (i.e. both wrapped strings and their builders).

like image 71
Rex Kerr Avatar answered Oct 01 '22 14:10

Rex Kerr