Nested quotes in string with OCaml?

Viewed 930

I have been trying to pass a string as an argument that has double quotes in it. For example, I want the input to be a string saying I am programming in "OCaml":

"I am programming in "OCaml""

That is what i want to be able to pass. I tried using some lexical conventions but had no luck. I don't want to change the quotes around OCaml.

I have tried

"\"I am programming in "OCaml"\"

And a couple of variations of this. Any tips on how to pass it?

2 Answers

You should escape the quotes inside the string, as with any other character that needs escaping, not the delimiters themselves:

"I am programming in \"OCaml\""

Alternatively, you can use a quoted string, which does not require (or allow) escaping and might be closer to what you were trying for:

{|I am programming in "OCaml"|}

For more details, see the section on string literals in the OCaml manual.

Here are a couple of literal strings containing double quotes:

"I am programming in \"OCaml\""
{|I am programming "OCaml"|}
Related