Forums

Doubling the ' token

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Doubling the ' token

Postby Ajrat » Tue Dec 18, 2012 12:40 am

Why the behavior is used for a ' token in raw strings like Python, but not in C#?
Python:
Code: Select all
r'\'text' # out: '\'text'

С#(double a ' token intro a string by doubling the ' token):
Code: Select all
@'''text'  // out: ''text'

I tried the sharp'@""' notation but it's breakin
Sorry for my bad English.
Ajrat
 
Posts: 8

Re: Doubling the ' token

Postby hopscc » Tue Dec 18, 2012 7:06 am

Theres no escaped ' for a string token in cobra because its unnecessary.
There is a choice of one of two delimiters for a string ('' or "")

If you want a ' (or a ") in a string you can use the other set of string delimiters
"'text" #out: 'text
'"text' #out: "text
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Doubling the ' token

Postby nerdzero » Tue Dec 18, 2012 9:05 am

Well, there is escaped quotes in case you need to include both kinds of quotes in your string, right?

'\'text' #out: 'text
'"\'text' #out: "'text
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Doubling the ' token

Postby Ajrat » Tue Dec 18, 2012 9:29 am

Thanks all!
Sorry for my bad English.
Ajrat
 
Posts: 8

Re: Doubling the ' token

Postby hopscc » Tue Dec 18, 2012 4:30 pm

Perhaps I misunderstood the question/issue.

Using the r prefix on a string treats it as a 'raw' or totally unprocessed string so there will be no escape char processing done on the contents.
any backslashes/escapes will then appear in the result
print '\'text'  # 'text
print "\"text" # "text
print r'\'text' #\'text
print r"\"text" # \"text

The first two are 'normal' strings ( escape processing and substitution (expression interpolation) done - substitution not used here)
the last two use raw strings so output as-is.


looks like the C# equivalent (@'????' ) uses doubled delimiters to indicate insertion of a delimiter - different approach.
cobra uses change of enclosing delimiter ('' vs "" preferably) or escaped delimiter in string
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Doubling the ' token

Postby Charles » Tue Dec 18, 2012 6:00 pm

So in summary, for raw strings, you can switch between delimiters (" vs. ') and in those rare instances where you need to include both " and ' in a raw string, you can concatenate two string literals with +.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Doubling the ' token

Postby Ajrat » Thu Dec 20, 2012 3:14 am

Why choose the way Python?
Sorry for my bad English.
Ajrat
 
Posts: 8

Re: Doubling the ' token

Postby hopscc » Thu Dec 20, 2012 5:19 am

Because its better :)

Swapped delimiters are noticeably easier to read/recognise than embedded doubled punctuation.
I'd say (arguably) so are escaped characters.....

Perhaps not inconsequentially they are also slightly easier to implement correctly..
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 12 guests