Single quotes in string 
Author Message
 Single quotes in string

If you want to have the word "don't" printed, you can do it like this:

  character(len=5) :: s
  s = "don't"
  print *, s

The single quote is recognised as a character, because double quotes
are used to bracket the text string. The reverse, 'don"t' also works,
although you might not want that.

However, this does not work:

  print '("don't")'

- you need two singles, as

  print '("don''t")'

What explains the difference? Where is the rule on this in the
F90 standard? I would have thought that the inner string in the
format description is just that, a string.


*** Echelon, bomb, sneakers, GRU: swamp the snoops with trivia! ***



Wed, 18 Jun 1902 08:00:00 GMT  
 Single quotes in string

Quote:

> However, this does not work:
>   print '("don't")'
> - you need two singles, as
>   print '("don''t")'

> What explains the difference? Where is the rule on this in the
> F90 standard? I would have thought that the inner string in the
> format description is just that, a string.

The rule is in section 4.3.2.1, character type.  I won't bother to type
it in word for word.  It just says that, as you have noticed, you need
to double quotes in a character literal if they are the same kind of
quotes as the delimitter.

As to why: I assume that you understant why the quote needs to
be doubled in

   'don''t'

If you just had

   'don't'

there is no way to know that the apostrophe is intended to be
part of the string instead of a delimiter.  Since *ANYTHING*
(well, at least anything printable - some subtle issues about
the non-printable stuff, which aren't relevant here) can be
in a quoted string, things can be ambiguous if you don't have
this rule.

I think your confusion is that you think there are such things
as nested strings.  There aren't.  Inside of a string delimitted
by single quotes (or apostrophies if you prefer that term), the
double quote is just a character like any other.  It isn't special.
In particular, note that you don't have to have an even number of
them.  So

  '("don'

is a perfectly fine string.  How is the compiler supposed to know that you
didn't intend that?  Well, in this case, it is true that the statement
doesn't parse correctly, but in general it is possible to have
ambiguities.

The doubling business is pretty ugly in bad cases.  I think that

  ''''

is a pretty confusing way to write a string consisting of a single
quote, and it can get worse than that.  With the option of using
either kind of quote for a delimitter, f90 allows you to avoid the
doubling in the simple cases.  Yes, you can come up with samples
where doubling (or quadrupling) is still needed. I personally
prefer to break things up to avoid the doubling.  You can usually
do so by making things just a little longer.  Anyway, you can
avoid the severe cases (where you'd need the quadrupling).  I find
it easier to read an extra line or 2 than to visually parse the
bad cases.

For example, an alternate way to write don't without doubling
quotes would be.

  write (*,'(a)') "don't"

--
Richard Maine



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Repost:Single quotes in string

2. single-quoted string conversion to triple-quoted string

3. parsing lines with quoted strings as single fields

4. inserting single quotes inside a smallTalk string

5. using gsub on a string containing single quotes

6. Making strings with single/double quotes safe for MySQL Insert/Update

7. Putting single quotes inside Prolog strings

8. replace single quote to double quote with an example

9. MySQLdb and strings with quotes / long strings

10. How to print out single quoted columns?

11. gawk win32 binary & single quote invalid char

12. Substituting single quotes from Perl via shell using awk and gsub

 

 
Powered by phpBB® Forum Software