[erlang-questions] Re: How to remove \ from string

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Wed Apr 6 19:46:45 CEST 2011


On Wed, Apr 6, 2011 at 19:36, Muhammad Yousaf <muhammad.yousaf@REDACTED> wrote:
>
> Thanks a lot but i only want to remove backslash not double quotes
> for example
> Val = <<"{get,\"user\",\"wilson\",\"lname\"}">> ,
> i need Val= <<"{get,"user","wilson","lname"}">>
> what i am doing with your help

Oh, then it is because you have a misconception of what \" actually
means. The problem is that to encode a string, we enclose it in
quotes, like "Hello". But when we want to encode a "-character by
itself, we need some way to tell the parser that this is actually the
"-character and not something that marks the end-of-string. So, we
adopt a convention: When we prefix by a backslash, \", the parser
treats it as an " rather than the end-of-string.

Eshell V5.8.4  (abort with ^G)
1> Val = <<"{get,\"user\",\"wilson\",\"lname\"}">>.
<<"{get,\"user\",\"wilson\",\"lname\"}">>
2> io:format("~s~n", [Val]).
{get,"user","wilson","lname"}
ok

As you can see, the quotes are just prefixed by a backslash for its
internal representation, and in the output, we get the string you
actually want to output.

In conclusion: It is a parser-technicality that we need some way to
distinguish the "-char from the "-end-of-string and we have adopted a
convention in order to carry out the discrimination.

-- 
J.



More information about the erlang-questions mailing list