- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to define a string literal bounded by double quotes as a raw string?
Sun, 2010-03-07, 11:02
For example, the string is "abc"(including two double quotes)
val str = """\"abc\""""
The above expression cannot work at all.
Sun, 2010-03-07, 11:37
#2
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@yahoo.com.cn> wrote:
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@yahoo.com.cn> wrote:
For example, the string is "abc"(including two double quotes)
val str = """\"abc\""""
The above expression cannot work at all.
Sun, 2010-03-07, 11:47
#3
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?
Sun, 2010-03-07, 11:57
#4
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@yahoo.com.cn> wrote:
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@yahoo.com.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?
Sun, 2010-03-07, 12:07
#5
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.
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.
>
>
>
>