Friday, November 5, 2010

Studying F# : String literal

In Ruby, there are two types for String literal. The first one is the literal not-escaping at all:
irb(main):001:0> s1 = "Hello World\n"
=> "Hello World\n"
irb(main):002:0> print s1
Hello World
=> nil
And the second one is escaping all:
irb(main):003:0> s2 = 'Hello World\n'
=> "Hello World\\n"
irb(main):004:0> print s2
Hello World\n=> nil

In F#, String literal is only using double-quote and this way is not escape at all.
> let s1 = "Hello World\n";;

val s1 : string = "Hello World

> printf "%s" s1;;
Hello World
val it : unit = ()

How do we escape all? The answer is using @ before double-quote, like below:
> let s2 = @"Hello World\n";;

val s2 : string = "Hello World\n"

> printf "%s" s2;;
Hello World\nval it : unit = ()

No comments:

Post a Comment