Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ERROR: syntax: cannot juxtapose string literal" when ending a triple-quoted string literal with a quote

Tags:

julia

I'm trying to create a string literal representing a CSV file with quoted fields. The intended CSV looks like this:

"a","b"
"1","2"

Triple quotes work if I want a newline character at the end of the string:

julia> """
       "a","b"
       "1","2"
       """
"\"a\",\"b\"\n\"1\",\"2\"\n"

But if I try to make a string without the newline character at the end, then I get a syntax error:

julia> """
       "a","b"
       "1","2""""
ERROR: syntax: cannot juxtapose string literal

Is there a simple way to get around this?

As an aside, note that there is no syntax error when you start the string-literal with a quote:

julia> """"a","b"
       "1","2"
       """
"\"a\",\"b\"\n\"1\",\"2\"\n"
like image 939
Cameron Bieganek Avatar asked Sep 18 '19 19:09

Cameron Bieganek


1 Answers

The issue is that this by itself is a valid string literal:

"""
"a","b"
"1","2"""

When you follow that with another " the parser thinks "woah, you can’t just follow a string with another string". You can force it to not consider the quote after 2 as part of a closing """ sequence by escaping it with \:

"""
"a","b"
"1","2\""""

At the start of the string, there's no such issue since the first three " characters are taken to start a string literal and the following " must just be a quote character inside of the string, which is what you want.

I'm not sure what you would consider the best solution to be. The options are:

  1. Escape the " at the end;
  2. Put the closing """ on a separate line.

The latter seems better to me, but it's your call.

See the Julia Docs for other examples on Triple-Quoted String Literals.

like image 88
StefanKarpinski Avatar answered Nov 10 '22 11:11

StefanKarpinski