Single Quote String String Interpolation

Interpolation within single quotes

You can use %{text contains "#{search.query}"} if you don't want to escape the double quotes "text contains \"#{search.query}\"".

Single quote string string interpolation

You cannot use string interpolation with single-quoted strings in Ruby.

But double-quoted strings can!

from = "'Name of Person' <#{ENV['EMAIL']}>"

But if you want to keep your double-quotes wrapping the Name of Person, you can escape them with a backslash \:

from = "\"Name of Person\" <#{ENV['EMAIL']}>"

Or use string concatenation:

from = '"Name of Person" <' + ENV['EMAIL'] + '>'
# but I find it ugly

Why does single quote not work with string interpolation in ruby

Ruby doesn't interpret single-quoted strings.

This might seem like a limitation at first, but it's actually a nice feature. It allows you to enter many characters without having to escape them, which results in more legible code:

file = 'C:\foo\bar\baz.txt'

# as opposed to:

file = "C:\\foo\\bar\\baz.txt"

Or when having a string about string interpolation itself: (note that Stack Overflow's syntax highlighting is misleading – there's no interpolation)

string = 'In Ruby, you can write "1 + 2 = #{ 1 + 2 }" to get "1 + 2 = 3".'

# instead of:

string = "In Ruby, you can write \"1 + 2 = \#{ 1 + 2 }\" to get \"1 + 2 = 3\"."

Apart from '...' and "...", Ruby also has %q(...) and %Q(...) style string literals (the former without, the latter with interpolation). This is especially useful if your string contains both, single and double quotes:

string = %q(A string containing '...' and "...")

You can even pick your own delimiter: (again, the syntax highlighter can't keep up)

string = %q@A string containing '...', "..." and (...)"@

And finally, you can mix and match different string literal styles:

string = %q(foo) 'bar' "baz"
#=> "foobarbaz"

Why Does String Interpolation Only Work In Double Quotes PHP

The obvious answer is that there are times when you might not want variables to be interpreted in your strings.

Take, for example, the following line of code:

$currency = "$USD";

This produces an "undefined variable" notice and $currency is an empty string. Definitely not what you want.

You could escape it ("\$USD"), but hey, that's a faff.

So PHP, as a design decision, chose to have double-quoted strings interpolated and single-quoted strings not.

powershell: newline in a single-quoted string?

Indeed, PowerShell's verbatim strings ('...', i.e. single-quoted) support only one escape sequence: '', which allows you to embed a verbatim '

Therefore, your choices are:

  • Either: Embed a verbatim (actual) newline in your '...' string.

  • Or - and this applies to any character you want to generate programmatically rather than verbatim - use string concatenation (+) or a templating approach via -f, the format operator

The following examples show these techniques:

'--- verbatim'
# Note: If the enclosing script uses Windows-style CRLF newlines,
# so will this string ("`r`n").
'line1
line2'

'--- concatenation'
# Note: In lieu of "`n" you may use [char] 10
# For Windows-style CRLF newlines, use "`r`n" or + [char] 13 + [char] 10
'line1' + "`n" + 'line2'

'--- -f operator:
'line1{0}line2' -f "`n"

If embedding verbatim newlines is an option, you may also use a verbatim here-string for better readability, which has the added advantage of not having to escape embedded ' characters:

# Here-string
@'
line1
line2
No escaping of ' needed.
'@

As with regular verbatim strings, it is the newline format of the enclosing script (Windows-style CRLF vs. Unix-style LF) that determines the format of the newlines used in the string.

C# How to wrap date string in single quote inside the string's double quotes

Use string.Format, or on C#6+ string interpolation:

// String format
string.Format("'{0}'", DateTime.Now.ToString("yyyy-MM-dd"));

// Interpolation
$"'{DateTime.Now.ToString("yyyy-MM-dd")}'";

Result:

'2017-03-30'

Double vs single quotes

" " allows you to do string interpolation, e.g.:

world_type = 'Mars'
"Hello #{world_type}"

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.



Related Topics



Leave a reply



Submit