This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How to define a string literal bounded by double quotes as a raw string?

5 replies
ZhangQidi
Joined: 2009-12-25,
User offline. Last seen 42 years 45 weeks ago.

For example, the string is "abc"(including two double quotes)

val str = """\"abc\""""

The above expression cannot work at all.

Caoyuan
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.
Re: How to define a string literal bounded by double quotes as

val str = """"abc""""

On Sun, Mar 7, 2010 at 6:02 PM, ZhangQidi wrote:
> For example, the string is "abc"(including two double quotes)
>
> val str = """\"abc\""""
>
> The above expression cannot work at all.
>
>
>
>

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
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).

Thus:

scala> "\"\"not raw\"\""
res0: java.lang.String = ""not raw""

scala> """""raw"""""
res1: java.lang.String = ""raw""

scala> "\"not raw\""
res2: java.lang.String = "not raw"

scala> """"raw""""
res3: java.lang.String = "raw"

  --Rex

On Sun, Mar 7, 2010 at 5:02 AM, ZhangQidi <zhangqidicn [at] yahoo [dot] com [dot] cn> wrote:
For example, the string is "abc"(including two double quotes)

val str = """\"abc\""""

The above expression cannot work at all.




ZhangQidi
Joined: 2009-12-25,
User offline. Last seen 42 years 45 weeks ago.
Re: How to define a string literal bounded by double quotes as

Well... my scala version is 2.7.7, jvm1.6, the compiler doesn't allow the following:

val str = """"abc""""

error: unterminated string

is it a bug or something?

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: How to define a string literal bounded by double quotes as
Ah, sorry, this was changed in 2.8; there it works the more useful way.

In 2.7.7, you are out of luck--if you want to add quotes to your string you'll need to use some other method:

scala> "\"" + """raw""" + "\""
res0: java.lang.String = "raw"

scala> def quote(s: String) = "\"" + s + "\""
quote: (String)java.lang.String

scala> quote("""raw""")
res1: java.lang.String = "raw"

scala> def quotes(s: String, n: Int) = ("\""*n) + s + ("\""*n)
quotes: (String,Int)java.lang.String

scala> quotes("""raw""",5)
res2: java.lang.String = """""raw"""""

  --Rex

On Sun, Mar 7, 2010 at 5:43 AM, ZhangQidi <zhangqidicn [at] yahoo [dot] com [dot] cn> wrote:
Well... my scala version is 2.7.7, jvm1.6, the compiler doesn't allow the following:

val str = """"abc""""

error: unterminated string

is it a bug or something?

ZhangQidi
Joined: 2009-12-25,
User offline. Last seen 42 years 45 weeks ago.
Re: How to define a string literal bounded by double quotes as

I see. It's not something serious, and there's many walk arounds. I just feel it odd.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland