In Java, is there a way to write a string literal without having to escape quotes?
The answer is no, and the proof resides in the Java Language Specification:
StringLiteral:
"StringCharacters"
StringCharacters:
StringCharacter
| StringCharacters StringCharacter
StringCharacter:
InputCharacter but not " or \
| EscapeSequence
As you can see a StringLiteral
can just be bound by "
and cannot contain special character without escapes..
A side note: you can embed Groovy inside your project, this will extend the syntax of Java allowing you to use '''multi line string '''
, ' "string with single quotes" '
and also "string with ${variable}"
.
Can I convert a C# string value to an escaped string literal?
There's a method for this in Roslyn's Microsoft.CodeAnalysis.CSharp package on NuGet:
private static string ToLiteral(string valueTextForCompiler)
{
return Microsoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral(valueTextForCompiler, false);
}
Obviously, this didn't exist at the time of the original question, but it might help people who end up here from Google Search.
How to write string literals in python without having to escape them?
Raw string literals:
>>> r'abc\dev\t'
'abc\\dev\\t'
Escaping verbatim string literals
The problem is that not all the strings you are concatenating are verbatim string literals, only the first portion of the concatenation is.
In other words,
@"SELECT value1, '"
is the only verbatim literal in the entire statement to build the final string.
You would need to add @ in front of the rest of your strings to make them all verbatim.
Which would make it look like:
String formLookupPull = @"SELECT value1, '"+tableName+ @"', '"+columnName+ @"' FROM lkpLookups WHERE ""table"" = '" + tableName + @"' and ""field"" = '" + columnName + @"';";
How to use \ in a string without making it an escape sequence - C#?
You can use Verbatim String Literals:
//Initialize with a regular string literal.
string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0";
// Initialize with a verbatim string literal.
string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0";
↑
Escape status within a string literal as argument of `String#tr`
Escaping in tr
The first argument of tr
works much like bracket character grouping in regular expressions. You can use ^
in the start of the expression to negate the matching (replace anything that doesn't match) and use e.g. a-f
to match a range of characters. Since it has control characters, it also does escaping internally, so you can use -
and ^
as literal characters.
print 'abcdef'.tr('b-e', 'x') # axxxxf
print 'abcdef'.tr('b\-e', 'x') # axcdxf
Escaping in Ruby single quote strings
Furthermore, when using single quotes, Ruby tries to include the backslash when possible, i.e. when it's not used to actually escape another backslash or a single quote.
# Single quotes
print '\\' # \
print '\d' # \d
print '\\d' # \d
print '\\\d' # \\d
# Double quotes
print "\\" # \
print "\d" # d
print "\\d" # \d
print "\\\d" # \d
The examples revisited
With all that in mind, let's look at the examples again.
'\\'.tr('\\', 'x') #=> "x"
The string defined as '\\'
becomes the literal string \
because the first backslash escapes the second. No surprises there.
'\\'.tr('\\d', 'x') #=> "\\"
The string defined as '\\d'
becomes the literal string \d
. The tr
engine, in turn uses the backslash in the literal string to escape the d
. Result: tr
replaces instances of d
with x.
'\\'.tr('\\\d', 'x') #=> "x"
The string defined as '\\\d'
becomes the literal \\d
. First \\
becomes \
. Then \d
becomes \d
, i.e. the backslash is preserved. (This particular behavior is different from double strings, where the backslash would be eaten alive, leaving only a lonesome d
)
The literal string \\d
then makes tr
replace all characters that are either a backslash or a d
with the replacement string.
Can I escape a double quote in a verbatim string literal?
Use a duplicated double quote.
@"this ""word"" is escaped";
outputs:
this "word" is escaped
Is it possible to insert escape sequence in a raw string literal?
While it's usually best to stick to one type of literal or the other, you can mix raw and non-raw literals in concatenation:
auto u8 = u8"UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422 \n";
auto u8Rs = u8R"u8R(UTF-8 encoded string literal: )u8R" u8"\u041F\u0420\u0418\u0412\u0415\u0422" u8R"u8R(
some additional stuff I want to add
to the previous string literal
because requirements slightly changed
or something)u8R";
Yes, it's ugly. I would seriously consider whether it's uglier than the alternative of a single non-raw literal. In the case of saving vertical editor space, I'd say don't. Use the raw literal and let people assume that what they see is exactly what they get rather than hiding extra newlines.
Related Topics
How to Overload Operators for Built-In Types Like Int or Float
How Do Sizeof(Arr)/Sizeof(Arr[0]) Work
Function Pointers Casting in C++
How to Simulate "Press Any Key to Continue"
Change Floating Point Rounding Mode
How to Get a "Useful" C++ Binary Search Algorithm
How to Initialize a Const Field in Constructor
Extending the C++ Standard Library by Inheritance
Why Does Long Long N = 2000*2000*2000*2000; Overflow
Check If a String Is Palindrome
Porting Clock_Gettime to Windows
Using Namespace Std; in a Header File
How to Include Header Files in Gcc Search Path
Get Address Family from Socket. Linux
How to Switch Between Blas Libraries Without Recompiling Program