Replace special qoutes with normal - vb.net

In VB.NET how do I replace special opening and closing double quotes (“ and ”) with ASCII quotes (").
Ive tried
s = s.replace("“", """")
but it seems that Visual Studio consider the “ quote in my code to be a normal quote leaving me with an invalid statement.

Unfortunately VB.NET doesn't support escape sequences but you can use ChrW() to specify code point:
s = s.Replace(ChrW(&H201C), """")
That's for “, code for ” is &H201D. Note that using code points you're free to search & replace any Unicode character (not just what VB.NET has an escape for - like ").
For complete list see: http://unicode-table.com/en/

If you want to use a quotation mark inside a string, VB doesn’t know whether the quotation mark is supposed to end the string or not. In C#, this would be fixed by escaping the quotation mark, i.e. in place of """ you’d write "\"". In VB, the same is done by doubling the quotation mark, i.e. """".
Back to your curly quote. The same as for straight quotes applies according to the VB language specification (¶1.6.4). So to write a curly quote in code, try the following:
s = Replace(s , "““", "“")
a second way: s = Replace(s , ChrW(&H201C), "“")

Related

Why does QUOTE_IDENT doubles the embedded quotation mark

The Redshift doc for the QUOTE_IDENT function says
The QUOTE_IDENT function returns the specified string as a string in double quotation marks so that it can be used as an identifier in a SQL statement. Appropriately doubles any embedded double quotation marks.
I understand the first part about surrounding a string with double quotation marks. But when is it appropriate to double embedded quotation marks? Why is that useful?
when is it appropriate to double embedded quotation marks? Why is that useful?
It's always appropriate. It's useful because the output of QUOTE_IDENT is often embedded into a larger string. As such, you then have a quoted string inside the larger string and you detect the end of such a string by finding a single double quote. I.e. assume we pass AB"CD to QUOTE_IDENT. We receive back "AB""CD". We then embed that into:
SELECT "AB""CD" FROM Foo
And when that's parsed later, we find a single identifier AB"CD in the SELECT list.

How to put a double quote within two double quotes in a batch file and how to escape double quote in VBA script file

So I want to echo out a line which contains just three double quotes. If I print two double quotes it just works fine and prints it (I believe the reason is that there is a corresponding closing quote) and double quotes in multiples of 2 work just fine but I'm unable to echo an odd number of them.
And from my searching the escape sequence character for double quote is double quote itself and it isn't getting me the desired result.
The command I'm running:
echo Item.Subject = Replace(Item.Subject, """, "-")>> try.xyz
expecting """ with this.
I've tried
"\""
"\\""
"^""
I am generating a bas file for importing in Outlook. And MC ND has corrected my mistake in the VBA file that I'm generating too. The proper way is to print 4 double quotes to do the escaping for the double quote from VBA script point of view and hence normal printing of 4 double quotes would solve my problem. The second double quote would be to escape the third one and the first and the last are to mention the replacement string from MC ND's answer.
Thanks a lot MC ND you've enlightened me. :-P
PS:
MC ND has given a much detailed explanation than this question deserved, so I extended the question.
Assumption: your batch code is generating a vbs file (or similar)
Your problem is not to echo the three quotes. Your problem is that in VBScript a double quote enclosed in double quotes needs to be escaped (from the vbs point of view), and to do so you need to double it, that is, you need not three but four quotes, the two that delimit the string and the two that means a escaped double quote
>>"try.xyz" echo Item.Subject = Replace(Item.Subject, """", "-")
note: I've moved the redirectioin to the start of the line to avoid problems with lines that could end in digits that could be parsed as a request to redirect a specific numbered stream
Assumption: Your code is NOT generating a vbs file and the quote escaping is what you indicate.
Then your problem is that the odd number of quotes is fooling the batch parser that keep an unclosed string that will include the redirection inside the data to echo, instead of writing to the output file
You can change the redirection to the start of the line as in the previous sample (at in this case it will work, the echo is still not correct), or, you can escape (from the batch point of view) the first quote so it is not considered double quote and to end with a properly quoted string, not including the redirection in the output
>>"try.xyz" Item.Subject = Replace(Item.Subject, """, "-")
echo Item.Subject = Replace(Item.Subject, ^""", "-") >> "try.xyz"
rem Best use both
>>"try.xyz" Item.Subject = Replace(Item.Subject, ^""", "-")
Why not to escape the inner quote? As it already is inside a quoted string, the escape will fail. We can escape the first or the last (in this case), not the inner one.
Assumption: Your code is not enclosed in parenthesis (for, if, ... ) inside the batch code
Then your code do not have any other problem,BUT if the assumption is wrong, then there is an additional problem. The closing parenthesis you are echoing will be seen by the batch parser as the closing parenthesis of the current block. To avoid it, you need to escape (from the batch parser point of view) the closing parenthesis.
(
....
>>"try.xyz" echo Item.Subject = Replace(Item.Subject, """", "-"^)
....
)

Remove double quote only when inside another double quote (VB.NET)

I'm using the library JSON.NET to bring a JSON string from the web, but the problem is that I get a double quote within a string.
the string that comes from the web is as follows
{"accionObjeto":"post","accionTipo":"comentario","ts":"02:48:55","nick":"seba123neo","userId":"1180918","id":15521634,"accion_name":"Hola","url":"","titulo":"Hola como" estas"}
the string is perfect except the end
here is the problem
"titulo":"Hola como" estas"
I have to remove that double quote, because otherwise the JSON is "invalid"
I've looked everywhere but can not find how to do this, I need only to erase the double quote, but not erase all other quotes in the entire string.
Thanks for your help.
It is not clear from your question whether you are generating the JSON string or if you are downloading it from the web. If you are creating it and the library is not escaping strings correctly, consider escaping them yourself.
This is a list of valid escapes
\b Backspace (ascii code 08)
\f Form feed (ascii code 0C)
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\' Apostrophe or single quote
\" Double quote
\\ Backslash caracter
As you can see you would have to escape a double quote by \". Before you code it yourself, look closer into the library you are using. I would be astonished, if it did not provide such functionality.

what characters should be escaped in sql string parameters

I need a complete list of characters that should be escaped in sql string parameters to prevent exceptions. I assume that I need to replace all the offending characters with the escaped version before I pass it to my ObjectDataSource filter parameter.
No, the ObjectDataSource will handle all the escaping for you. Any parametrized query will also require no escaping.
As others have pointed out, in 99% of the cases where someone thinks they need to ask this question, they are doing it wrong. Parameterization is the way to go. If you really need to escape yourself, try to find out if your DB access library offers a function for this (for example, MySQL has mysql_real_escape_string).
SQL Books online:
Search for String Literals:
String Literals
A string literal consists of zero or more characters surrounded by quotation marks. If a string contains quotation marks, these must be escaped in order for the expression to parse. Any two-byte character except \x0000 is permitted in a string, because the \x0000 character is the null terminator of a string.
Strings can include other characters that require an escape sequence. The following table lists escape sequences for string literals.
\a
Alert
\b
Backspace
\f
Form feed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\"
Quotation mark
\
Backslash
\xhhhh
Unicode character in hexadecimal notation
Here's a way I used to get rid of apostrophes. You could do the same thing with other offending characters that you run into. (example in VB.Net)
Dim companyFilter = Trim(Me.ddCompany.SelectedValue)
If (Me.ddCompany.SelectedIndex > 0) Then
filterString += String.Format("LegalName like '{0}'", companyFilter.Replace("'", "''"))
End If
Me.objectDataSource.FilterExpression = filterString
Me.displayGrid.DataBind()

How can I write special character in VB code

I have a Sql statament using special character (ex: ('), (/), (&)) and I don't know how to write them in my VB.NET code. Please help me. Thanks.
Find out the Unicode code point for the character (from http://www.unicode.org) and then use ChrW to convert from the code point to the character. (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.)
For example, for the Euro sign (U+20AC) you'd write:
Dim euro as Char = ChrW(&H20AC)
The advantage of this over putting the character directly into source code is that your source code stays "just pure ASCII" - which means you won't have any strange issues with any other program trying to read it, diff it, etc. The disadvantage is that it's harder to see the symbol in the code, of course.
The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap
If you are passing strings to be processed as SQL statement try doubling the characters for example.
"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "
The '' double works with the other special characters as well.
The ' character can be doubled up to allow it into a string e.g
lSQLSTatement = "Select * from temp where name = 'fred''s'"
Will search for all records where name = fred's
Three points:
1) The example characters you've given are not special characters. They're directly available on your keyboard. Just press the corresponding key.
2) To type characters that don't have a corresponding key on the keyboard, use this:
Alt + (the ASCII code number of the special character)
For example, to type ¿, press Alt and key in 168, which is the ASCII code for that special character.
You can use this method to type a special character in practically any program not just a VB.Net text editor.
3) What you probably looking for is what is called 'escaping' characters in a string. In your SQL query string, just place a \ before each of those characters. That should do.
Chr() is probably the most popular.
ChrW() can be used if you want to generate unicode characters
The ControlChars class contains some special and 'invisible' characters, plus the quote - for example, ControlChars.Quote