Why is the # symbol added in front of the string when defining ADO.NET connection strings? - ado.net

For eg.
public string conString =#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\PersonDatabase.mdb";
I don't understand its use.What am I missing here?

The # creates a verbatim string literal, which ignores \ characters.
It means you don't need to write \\ in file paths. (which means your example is wrong)

Related

Special character in key in the conf file for Internationlization in play framework

I am trying to use the Internationalization feature of the Play Framework.
It involves the creation of a new conf file for each language that we want to support. Example for french we create a messages.fr file in the conf folder.
Inside it we define key-values like this:
Hello.World = 'Bonjour le monde'
Now the issue is that I have lines that contain characters like "," and "(" and if these are included in the key then we get the error in parsing from the MessageApi
Example
Hello.(World) = 'Bonjour (le monde)'
Here the "(" before and after World is throwing an error while parsing.
Anyone having any idea how we could achieve this?
Try to escape these special characters:
Hello.\(World\) = 'Bonjour (le monde)'
Other examples:
string_one = String for translation 1
string_two = one + one \= two
# String key with spaces
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Backslash in value should be escaped by another backslash
path=c:\\wiki\\ templates
Also, you can try to escape special characters by using Java Unicode:
Hello.\u0028World\u0029 = 'Bonjour (le monde)'
Reference - How to escape the equals sign in properties files

Password escapes in SQL Azure connection string

I had the bright idea of generating a secure password using KeePass password manager and give it to my DBA.
Now I constructed my Azure SQL connection string as follows
Server=tcp:something.database.windows.net,1433;Initial Catalog=SomeDbName;Persist Security Info=False;User ID=someuser;Password=[read later];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
The password contains at least the following characters, along with upper/lowercase/digits
/"|&#]<#
Error is: ArgumentException: Format of the initialization string does not conform to specification starting at index 151.
At index 151 I can see the semicolon after the username (which I named someuser in the redacted example but only contains lowercase and a digit). VS Code points column 151 right before the ;Password= sequence
Questions:
Does my SQL Azure password contain illegal characters?
Which ones need to be escaped?
How do I escape them?
[Edit]: a note about the double quotes, which must be always escaped in C# Strings. I buried the connection string under Azure App configuration tab. It means that the double quote is unlikely to be re-escaped. I believe EF Core currently reads double-quote correctly
Actually escaping is specified here: connection-strings
Simple test shows that for your password, it is enough to add single quotes:
var csBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
csBuilder.Password = "/\"|&#]<#";
var cs = csBuilder.ConnectionString;
Console.WriteLine(cs);
Password='/"|&#]<#'

How to create Swift variable with JSON literal string?

I want to create a string variable with a JSON string. Swift doesn't seem to allow me to do this, where I use ` or ' to wrap the JSON string to escape it.
var json = '{"variable":"hello world"}'
var json = `{"variable":"hello world"}`
Thanks
Swift String literals are always enclosed in double quotes " — see here in the documentation. There is no alternative syntax for string literals (like there is in Ruby for example)… at least as of Swift 1.2.
So if you need to put quotes in your string literal, you then need to escape them.
let json = "{\"variable\":\"hello world\"}"
(the only other alternative if you have a lot of quotes to escape would be to load the JSON string from a resource file for example. But then that's not a String literal anymore)

Insert keywords (double quotes) in mongodb using insert statement. (Escape sequence)

I want to insert a string that contains double quotes and single quotes as below.
db.collectionName.insert({"filedName" : "my field contains "double quotes" and 'single quotes' how to insert"}) ;
When I try insert above, it got error as my field contain double quotes, can some one tell me something like escape sequence to insert double quote?
Can't do as under my field also contain single quotes.
db.collectionName.insert({"filedName" : 'my field contains "double quotes" and 'single quotes' how to insert'}) ;
I think it depends on which context you use your code.
If it's in pure js (node.js for example) you can escape the quote char with \, like this :
db.collectionName.insert({"filedName" : "my field contains \"double quotes\" and 'single quotes' how to insert"}) ;
But in the HTML context it's not possible, you need to replace the double-quote with the proper XML entity representation, "
Consider using grave/accent (`, decimal ASCII code 96) for string enclosing quotes.
JSON allows unicode syntax using \u1234 format according to ECMA-404. Therefore you can use the following syntax to insert double quote and single quotes from MongoDB shell.
db.collectionName.insert({"filedName" : "my field contains \u0022double quotes\u0022 and \u0027single quotes\u0027 how to insert"})
Further, you can use this syntax to insert non-ASCII characters too. This special handling is required if we are forming the JSON manually. If the JSON is being serialized automatically from plain objects (e.g. POJOs) using a framework such as GSON, Xstream the required conversion would happen automatically while converting to JSON.

How to output """ in the "here docs" of scala?

In scala, "here docs" is begin and end in 3 "
val str = """Hi,everyone"""
But what if the string contains the """? How to output Hi,"""everyone?
Since unicode escaping via \u0022 in multi-line string literals won’t help you, because they would be evaluated as the very same three quotes, your only chance is to concatenate like so:
"""Hi, """+"""""""""+"""everyone"""
The good thing is, that the scala compiler is smart enough to fix this and thus it will make one single string out of it when compiling.
At least, that’s what scala -print says.
object o {
val s = """Hi, """+"""""""""+"""everyone"""
val t = "Hi, \"\"\"everyone"
}
and using scala -print →
Main$$anon$1$o.this.s = "Hi, """everyone";
Main$$anon$1$o.this.t = "Hi, """everyone";
Note however, that you can’t input it that way. The format which scala -print outputs seems to be for internal usage only.
Still, there might be some easier, more straightforward way of doing this.
It's a totally hack that I posted on a similar question, but it works here too: use Scala's XML structures as an intermediate format.
val str = <a>Hi,"""everyone</a> text
This will give you a string with three double quotation marks.
you can't
scala heredocs are raw strings and don't use any escape codes
if you need tripple quotes in a string use string-concatenation add them
You can't using the triple quotes, as far as I know. In the spec, section 1.3.5, states:
A multi-line string literal is a sequence of characters enclosed in triple quotes
""" ... """. The sequence of characters is arbitrary, except that it may contain
three or more consuctive quote characters only at the very end. Characters must
not necessarily be printable; newlines or other control characters are also permitted.
Unicode escapes work as everywhere else, but none of the escape sequences in
(§1.3.6) is interpreted.
So if you want to output three quotes in a string, you can still use the single quote string with escaping:
scala> val s = "Hi, \"\"\"everyone"
s: java.lang.String = Hi, """everyone