benteeuwen
benteeuwen

Reputation: 83

Scala string to array to string with quoted elements

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?

Upvotes: 2

Views: 8265

Answers (5)

Matta
Matta

Reputation: 43

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

Upvotes: 0

Chris K
Chris K

Reputation: 11925

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("'", "', '", "'")

Upvotes: 13

Chris K
Chris K

Reputation: 11925

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"
}

Upvotes: -1

elm
elm

Reputation: 20435

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.

Upvotes: 0

Hackaholic
Hackaholic

Reputation: 19771

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'

Upvotes: 0

Related Questions