简体   繁体   English

是否可以使用“普通”长字符串?

[英]Is it possible to use a "plain" long string?

In Julia, you can't store a string like that:在 Julia 中,您不能存储这样的字符串:

str = "\mwe"

Because there is a backslash.因为有一个反斜杠。 So the following allows you to prevent that:因此,以下内容可以防止这种情况发生:

str = "\\mwe"

The same occurs for "$, \n" and many other symbols. "$, \n"和许多其他符号也是如此。 My question is, given that you have a extremely long string of thousands of characters and this is not very convenient to treat all the different cases even with a search and replace (Ctrl+H), is there a way to assign it directly to a variable?我的问题是,鉴于你有一个包含数千个字符的极长字符串,即使使用搜索和替换 (Ctrl+H) 来处理所有不同的情况也不是很方便,有没有办法将它直接分配给一个多变的?

Maybe the following (which I tried) gives an idea of what I'd like:也许以下(我试过)给出了我想要的想法:

str = """\$$$ \\\nn\nn\m this is a very long and complicated (\n^$" string"""

Here """ is not suitable, what should I use instead?这里"""不合适,应该用什么代替?

Quick answer: raw string literals like raw"\$$$ \\\nn..." will get you most of the way there.快速回答:像raw"\$$$ \\\nn..."这样的原始字符串文字将帮助您完成大部分工作。

Raw string literals allow you to put nearly anything you like between quotes and Julia will keep the characters as typed with no replacements, expansions, or interpolations.原始字符串文字允许您在引号之间放置几乎任何您喜欢的内容,而 Julia 将保持键入的字符,没有替换、扩展或插值。 That means you can do this sort of thing easily:这意味着您可以轻松地完成此类操作:

a = raw"\mwe"
@assert codepoint(a[1]) == 0x5c  # Unicode point for backslash

b = raw"$(a)"
@assert codepoint(b[1]) == 0x25  # Unicode point for dollar symbol

The problem is always the delimiters that define where the string begins and ends.问题始终是定义字符串开始和结束位置的定界符。 You have to have some way of telling Julia what is included in the string literal and what is not, and Julia uses double inverted commas to do that, meaning if you want double inverted commas in your string literal, you still have to escape those:你必须有一些方法来告诉 Julia 什么包含在字符串文字中,什么不包含,而 Julia 使用双引号来做到这一点,这意味着如果你想在你的字符串文字中使用双引号,你仍然必须转义那些:

c = raw"\"quote"  # note the two backslashes
@assert codepoint(c[1]) == 0x22  # Unicode point for double quote marks

If this bothers you, you can combine triple quotes with raw , but then if you want to represent literal triple quotes in your string, you still have to escape those :如果这让您感到困扰,您可以将三重引号与raw结合使用,但是如果您想在字符串中表示文字三重引号,您仍然必须转义那些

d = raw""""quote"""  # the three quotes at the beginning and three at the end delimit the string, the fourth is read literally
@assert codepoint(d[1]) == 0x22  # Unicode point for double quote marks

e = raw"""\"\"\"""" # In triple quoted strings, you do not need to escape the backslash
@assert codeunits(e) == [0x22, 0x22, 0x22]  # Three Unicode double quote marks

If this bothers you, you can try to write a macro that avoids these limitations, but you will always end up having to tell Julia where you want to start processing a string literal and where you want to end processing a string literal, so you will always have to choose some way to delimit the string literal from the rest of the code and escape that delimiter within the string.如果这让您感到困扰,您可以尝试编写一个避免这些限制的宏,但您最终总是不得不告诉 Julia 您希望从何处开始处理字符串文字以及您希望在何处结束处理字符串文字,因此您将总是必须选择某种方式来从代码的 rest 中分隔字符串文字,并在字符串中转义该分隔符。

Edit: You don't need to escape backslashes in raw string literals in order to include quotation marks in the string, you just need to escape the quotes.编辑:您不需要为了在字符串中包含引号而转义原始字符串文字中的反斜杠,您只需要转义引号即可。 But if you want a literal backslash followed by a literal quotation mark, you have to escape both:但是如果你想要一个文字反斜杠后跟一个文字引号,你必须转义两者:

f = raw"\"quote"
@assert codepoint(f[1]) == 0x22  # double quote marks

g = raw"\\\"quote"  # note the three backslashes
@assert codepoint(g[1]) == 0x5c  # backslash
@assert codepoint(g[2]) == 0x22  # double quote marks

If you escape the backslash and not the quote marks, Julia will get confused:如果你转义反斜杠而不是引号,Julia 将会混淆:

h = raw"\\"quote"
# ERROR: syntax: cannot juxtapose string literal

This is explained in the caveat in the documentation .这在文档的警告中进行了解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM