如何从字符串 Scala 的前端和末尾删除引号

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

我有一个数据框,其中一些字符串在字符串的前后包含“”。

例如:

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

预期输出:

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

我试图只从字符串中删除 " 双引号。谁能告诉我如何在 Scala 中删除它?

Python 提供 ltrim 和 rtrim。在 Scala 中是否有与此等效的东西?

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

myString.substring(1, myString.length()-1) 将删除双引号。

  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)

结果:

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

+-------------------+
|              value|
+-------------------+
|                 hi|
|I am learning scala|
|                pls|
|               help|
+-------------------+

使用 expr, substringlength 函数,从 2length() - 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|
+-------------------+

试一试

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|
+-------------------+