Auto string escaping

Whats the correct way to convert a String to a String where all the special characters are escaped. I.e., I need to print some text into a gnuplot file. The format says I need to put quotation marks around strings. I of course can write such a function myself, but if the concept already exists in the library, I’m more happy to use that as it’s probably more correct than my first attempt.

This doesn’t exist yet.

it’s a neat exercise though

def escape(src: String, escape: Char, toEscape: Set[Char]): String = {
  val sb = new StringBuilder(src.length * 2)
  def go(i: Int): String =
    if(src.length >= i) sb.toString
    else {
      val ch = src(i)
      if (toEscape.contains(ch)) sb.append(escape)
      go(i + 1)


There’s different flavors of “escaping” (as well as definitions of “special characters”). You could check whether one of the commons-text variants fits your use case.

honestly I don’t really understand the escaping rules of gnuplot labels. I tend to avoid using anything escapable, but when writing an general purpose gnuplot interface, it ought to be robust.

I’m not familiar with gnuplot, but I’d hope that its escaping rules are within the range covered by commons-text.

Have you considered Scala/Java-native plotting like Breeze or JFreeChart as an alternative?

I started with scalaplot, but discovered that it was last updated for 2.11, and is buggy in 2.12. Some things work and somethings don’t. I filed several issues on scalaplot gitLab but no response came. So I decided that for my purpose I could simply write the gnuplot input file (It’s ugly but straightforward). I already had the code in Common Lisp, 20 or 30 lines, so I converted it. CL has auto-escaping built in, and gnuplot seems to be similar enough to CL’s assumptions, that I haven’t had any problems, although I’m sure there are differences which I have not encountered.

Breeze or JFreeChart? No I hadn’t looked into them. But perusing the docs, JFreeChart looks pretty sweet. Would you recommend using JFreeChart directly or through Breeze?

I have no experience with Breeze at all, and I’ve only used JFreeChart quite some time ago for rather pedestrian bar charts. My recollection is that it works fine and that the API is ok-ish but pretty javaesque. Personally I’d certainly check out whether Breeze fits my use case before I commit to raw JFreeChart.

1 Like

BreezeViz is just a wrapper for JFreeChart. There are several other plotting libraries for Scala. Vegas-Viz ( seems to be fairly well maintained and works well for web-based but my experience with desktop plotting was not positive. Netflix chose it for PolyNote though. I haven’t played with it, but EvilPlot ( also looks interesting. There are others as well.

I have been working on my own, SwiftVis2 (, but it is still in heavy flux and not that mature. I looked at several other options when I wrote up a little paper on SwiftVis2 (

As an interesting note, SwiftVis2 using Swing is much faster than using JavaFX/ScalaFX ( It is also faster than JFreeChart using the BreezeVis wrapper. My own plotting needs are odd because I’m often using scatter plots to display ring simulations with millions of particles, so the render speed has an impact on my workflow. Right now I have a student working on getting SwiftVis2 to work for Scala.js and Scala Native as well. That is part of why it is in heavy flux and we haven’t documented any of that work yet.