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

Related searches

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 ?

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

How can I trim beginning and ending double quotes from a string , If you need to also remove single quotes: String result = input_str. replaceAll("^[\"']+|[\"']+$", ""); To remove one or more double quotes from the start and end of a string in Java, you need to use a regex based solution: String result = input_str.replaceAll("^\"+|\"+$", ""); If you need to also remove single quotes:

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

Efficient way to remove "" from some words in a String : scala, I need to make a transformation on a string, which will remove "" from some words. Or how to write a regexp, which will remove quotes only from specific words? bin_range =pd.date_range(start='1/1/2000', end='12/31/2020', freq='1Y' ). Re: How to define a string literal bounded by double quotes as With raw strings, everything between triple-quotes is part of the string, _including_ quotes. The only problem is if you have 6 or more quotes in a row (6 quotes in a row = the empty string).

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

How to left-trim and right-trim strings in Scala, Remove all blank strings from Scala sequences. In a related “string utilities” note, here's a method that removes all blank strings from a Seq , List ,� January 6, 2016 July 19, 2018 Ayush Mishra JavaScript, Scala scala, scala.js 15 Comments on Why Scala.js is preferred language for front-end development 3 min read Reading Time: 4 minutes It becomes very painful, when I have to refactor a large Javascript code base, written by someone else.

Java Program to remove the leading and trailing quotes from a string, Firstly, let us consider a string with quotesString originalStr = \Demo Text\;Now, consider the following logic for the beginning quote.if� string - leading - scala trim quotes Trimming strings in Scala (4) Given you only want to trim off invalid characters from the prefix and the suffix of a given string (not scan through the entire string), here's a tiny trimPrefixSuffixChars function to quickly perform the desired effect:

>> String. I didn't want to go over the list more than once (although at the >> end I remove multiple separators with the replaceAll so it's actually 2 >> times). > Try name.view to get a "lazy" SeqView - then you can apply multiple > maps to it, without actually traversing the SeqView. > > Michał Bendowski >

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short solution from the book, Recipe 1.8, “Replacing Patterns in Scala Strings.” Problem. You want to search for regular-expression patterns in a Scala string, and replace them. Solution

Comments
  • 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?
  • 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?
  • @AndreyTyukin Yes. I would like to remove only the quotes that is in front and end of the string
  • Does this answer your question? Just replace ' by " in the regex: "\"a \"b\" c\"".replaceAll("(^\")|(\"$)", "").