如何从字符串Scala的开头和结尾删除引号

时间:2019-05-18 22:56:35

标签: regex string scala apache-spark trim

我有一个数据框,其中某些字符串在字符串的开头和结尾都包含“”。

例如:

+-------------------------------+
|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中的功能等效的吗?

3 个答案:

答案 0 :(得分:4)

使用 expr,子字符串 length 函数并从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|
+-------------------+

答案 1 :(得分:2)

  

如何从字符串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|
+-------------------+

答案 2 :(得分:2)

尝试

Number