2

In Scala, how can I convert a comma separated string to an array to a quoted string?

E.g. in R I'd use:

> bold_statement_string  <- "narcos,is,maybe,better,than,the,wire"
[1] "narcos,is,maybe,better,than,the,wire"
> bold_statement_array  <- strsplit(bold_statement_string, ',')[[1]]
[1] "narcos" "is"     "maybe"  "better" "than"   "the"    "wire"  
> cat(paste(shQuote(bold_statement_array), collapse = ','))
'narcos','is','maybe','better','than','the','wire'

In Scala, it worked with:

var bold_statement_string = "narcos,is,maybe,better,than,the,wire"
var bold_statement_array = bold_statement.split(',')
s"'${bold_statement_array.mkString("','")}'"
Out[83]:
'narcos','is','maybe','better','than','the','wire'

In python I hear there's always a pythonic way of doing it. Is there a more Scala-esk way of doing it? Or should I just rejoice in having found a solution, no matter if it could be more elegant?

5 Answers 5

13

First, in Scala we use val instead of var. We like to embrace the immutability thing where ever we can.

val bold_statement_string = "narcos,is,maybe,better,than,the,wire"

second you do not need to use string interpolation, mkString can take a prefix and a postfix which gives us:

bold_statement_string.split(",").mkString("'", "', '", "'")
Sign up to request clarification or add additional context in comments.

Comments

0

try this: for array:

scala> bold_statement_string.split(",") map {"\'%s\'".format(_)}
res8: Array[String] = Array('narcos', 'is', 'maybe', 'better', 'than', 'the', 'wire')

For String:

 scala> bold_statement_string.split(",") map {"\'%s\'".format(_)} mkString ","
res9: String = 'narcos','is','maybe','better','than','the','wire'

1 Comment

There is a three argument version of mkString that takes care of everything: bold_statement_string.split(',').mkString("'", "','", "'").
0

In the example proposed, splitting the string by comma and reconstructing the string with comma, results in the identical initial string. For the case of simply wrapping the string with single quotes, consider this use of mkString

bold_statement_string.mkString("'","","'")

which treats the string as a Seq[Char], and prepends and appends the single quote characters.

Comments

0

Here is the simplest way.

val bold_statement_string = "narcos,is,maybe,better,than,the,wire"
bold_statement_string
  .split(",")              // Split with comma
  .map("'" + _  + "'")     // Wrap with single quote
  .mkString(","))          // make string with comma

Comments

-1

Here is a more generic version that uses a typical Scala approach using map to quote the array elements and foldLeft to combine them back together.

val arr = bold_statement_string.split(",").map( v => s"'$v'" )

arr.foldLeft("")( (soFar,v) => if (v=="") v else soFar + ", " + v )
    // fold left starts with the value "" and then invokes the function
    // provided for every element in the collection which combines them
    // 
    // ("","'narcos'") goes to "'narcos'"
    // ("'narcos'", "'is'") goes to "'narcos', 'is'"
    // ("'narcos', 'is'", "'maybe'") goes to "'narcos', 'is', 'maybe'" 
    // and so on 
    // up to the last element giving us the target string
    // 'narcos', 'is', 'maybe', 'better', 'than', 'the', 'wire'

and here is a variant that works in the same way but rather than use if/else it uses the more functional Scala match.

bold_statement_string.split(",").map( v => s"'$v'" ).foldLeft("") {
  case ("",v) => v
  case (soFar,v) => s"$soFar, $v"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.