2

I have a dataframe where some strings contains "" in front and end of the string.

Eg:

+-------------------------------+
|data                           |
+-------------------------------+
|"john belushi"                 |
|"john mnunjnj"                 |
|"nmnj tyhng"                   |
|"John b-e_lushi"               |
|"john belushi's book"          |

Expected output:

+-------------------------------+
|data                           |
+-------------------------------+
|john belushi                   |
|john mnunjnj                   |
|nmnj tyhng                     |
|John b-e_lushi                 |
|john belushi's book            |

I am trying to remove only " double quotes from the string. Can some one tell me how can I remove this in Scala ?

Python provide ltrim and rtrim. Is there any thing equivalent to that in Scala ?

user3407267
  • 1,524
  • 9
  • 30
  • 57
  • 2
    What's the problem with `"\"hello\"world".replaceAll("\"", "")` or `"\"hello\"world".filterNot(_ == '"')`? And what does it have to do with `trim` - it seems that you are deleting quotes everywhere, also in the middle of the string? – Andrey Tyukin May 18 '19 at 23:00
  • From your edit, it did not become clearer why the solutions from the first comment wouldn't work. Do you want to preserve the quotes in the middle of the string? – Andrey Tyukin May 18 '19 at 23:06
  • @AndreyTyukin Yes. I would like to remove only the quotes that is in front and end of the string – user3407267 May 18 '19 at 23:12
  • Does [this](https://stackoverflow.com/questions/3315053/java-regex-to-remove-start-end-single-quotes-but-leave-inside-quotes) answer your question? Just replace `'` by `"` in the regex: `"\"a \"b\" c\"".replaceAll("(^\")|(\"$)", "")`. – Andrey Tyukin May 18 '19 at 23:14

3 Answers3

4

Use expr, substring and length functions and get the substring from 2 and length() - 2

val df_d = List("\"john belushi\"", "\"John b-e_lushi\"", "\"john belushi's book\"")
.toDF("data")

Input:

+---------------------+
|data                 |
+---------------------+
|"john belushi"       |
|"John b-e_lushi"     |
|"john belushi's book"|
+---------------------+

Using expr, substring and length functions:

import org.apache.spark.sql.functions.expr

df_d.withColumn("data", expr("substring(data, 2, length(data) - 2)"))
    .show(false)

Output:

+-------------------+
|data               |
+-------------------+
|john belushi       |
|John b-e_lushi     |
|john belushi's book|
+-------------------+
notNull
  • 30,258
  • 4
  • 35
  • 50
3

Try it

scala> val dataFrame = List("\"john belushi\"","\"john mnunjnj\"" , "\"nmnj tyhng\"" ,"\"John b-e_lushi\"", "\"john belushi's book\"").toDF("data")

scala> dataFrame.map { row => row.mkString.stripPrefix("\"").stripSuffix("\"")}.show

+-------------------+
|              value|
+-------------------+
|       john belushi|
|       john mnunjnj|
|         nmnj tyhng|
|     John b-e_lushi|
|john belushi's book|
+-------------------+
Dhirendra
  • 289
  • 3
  • 8
2

How to remove quotes from front and end of the string Scala?

myString.substring(1, myString.length()-1) will remove the double quotes.

  import spark.implicits._
val list = List("\"hi\"", "\"I am learning scala\"", "\"pls\"", "\"help\"").toDF()
list.show(false)
val finaldf = list.map {
  row => {
    val stringdoublequotestoberemoved = row.getAs[String]("value")

    stringdoublequotestoberemoved.substring(1, stringdoublequotestoberemoved.length() - 1)
  }
}
finaldf.show(false)

Result :

+--------------------+
|               value|
+--------------------+
|                "hi"|
|"I am learning sc...|
|               "pls"|
|              "help"|
+--------------------+

+-------------------+
|              value|
+-------------------+
|                 hi|
|I am learning scala|
|                pls|
|               help|
+-------------------+
Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121