Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:51:40+00:00 2026-06-10T07:51:40+00:00

How do I replace with \. Here is what im trying : def main(args:Array[String])

  • 0

How do I replace ” with \”.

Here is what im trying :

def main(args:Array[String]) = {      
  val line:String = "replace \" quote";
  println(line);
  val updatedLine = line.replaceAll("\"" , "\\\"");
  println(updatedLine);
}

output :

replace " quote
replace " quote

The output should be :

replace " quote
replace \" quote
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:51:42+00:00Added an answer on June 10, 2026 at 7:51 am

    Two more \\ does the job:

    scala>  line.replaceAll("\"" , "\\\\\"");
    res5: java.lang.String = replace \" quote
    

    The problem here is that there are two ‘layers’ escaping the strings. The first layer is the compiler, which we can easily see in the REPL:

    scala> "\""
    res0: java.lang.String = "
    
    scala> "\\"
    res1: java.lang.String = \
    
    scala> "\\\""
    res2: java.lang.String = \"
    
    scala> val line:String = "replace \" quote";
    line: String = replace " quote
    

    The second layer is the regular expression interpreter. This one is harder to see, but can be seen by applyin your example:

    scala>  line.replaceAll("\"" , "\\\"");
    res5: java.lang.String = replace " quote
    

    What the reg. exp. interpreter really receives is \”, which is interpreted as only “. So, we need the reg. exp. to receive \\”. To make the compiler give us \ we need to write \\.

    Let’s see the unescaping:

    • The right case: \\\” the compiler sees \”, the regular expression sees \”.
    • The wrong case: \\” the compiler sees \”, the regular expression sees “.

    It can be a bit confusing despite being very straight forward.

    As pointed by @sschaef, another alternative it to use “”” triple-quoting, strings in this form aren’t unescaped by the compiler:

    scala>  line.replaceAll("\"" , """\\"""");
    res6: java.lang.String = replace \" quote
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that represents an html document. I'm trying to replace text
total newbie here. i was trying to replace a character in char * but
Here is what I am trying to achieve: I want to replace void some_function(int,
I'm trying to replace the contents inside braces on a string with preg_replace_callback .
I am trying to replace a line Number 3 of text in this File
Here is my query (I replaced table names with generic ones). I am trying
replace newline seems to be a question asked here and there like hundred times
I am attempting to replace multiple lines using sed on a Linux system Here
Here is my PHP code.. $input = zergling-light $output = str_replace('-', ' (', $input).);
I find out how to replace the busy cursor here: http://livedocs.adobe.com/flex/3/html/cursormgr_3.html However, how do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.