Single string on multiple lines without `\n`

Is there a way to continue a single string on multiple lines without inserting \n into the string?

For example

"a#some character here
b"
"ab"

The only methods I currently know of is

string("a",
    "b")

I was wondering if there was an even more succinct shortcut.

1 Like
julia> """a
       b
       c
       """
"a\nb\nc\n"

Edit: uh… that’s not what you were asking for. I don’t think you can do that with a single string literal, no.

2 Likes

What I meant was, can I type the string on multiple lines (for code formatting purposes) without there being any \n in the string?

Technically

julia> "a"*
       "b"*
       "c"
"abc"

is more succinct. Otherwise see my edit above :wink:

4 Likes

Wow I feel really stupid for not having realized that, thanks.

In retrospect, what I was looking for was not necessarily something more “succinct”, but something for which I wouldn’t have to stick string into the first line forcing me to reformat everything, your solution satisfies that, thanks.

Alternatively:

macro W_str(s)
    :(replace($(esc(s)), r"\n+" => " "))
end
julia> W"""
       a
       b
       c
       """
"a b c "
7 Likes

Old topic, but worth updating I think. As per Julia 1.7 release notes: " A backslash (\ ) before a newline inside a string literal now removes the newline while also respecting indentation"

julia> "a \
       b \
       c"
"a b c"
9 Likes