使用 OCaml 在字符串中嵌套引号? [英] Nested quotes in string with OCaml?
问题描述
我一直试图将一个字符串作为参数传递,其中包含双引号.例如,我希望输入是一个字符串,表示我正在OCaml"中编程:
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""
这就是我希望能够通过的.我尝试使用一些词汇约定,但没有运气.我不想更改 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 am programming in "OCaml"\"
还有一些变体.关于如何通过它的任何提示?
And a couple of variations of this. Any tips on how to pass it?
推荐答案
你应该转义字符串里面的引号,就像任何其他需要转义的字符一样,而不是分隔符本身:
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"|}
有关详细信息,请参阅有关字符串文字的部分在 OCaml 手册中.
For more details, see the section on string literals in the OCaml manual.
这篇关于使用 OCaml 在字符串中嵌套引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!