简体   繁体   English

如何在VB代码中编写特殊字符

[英]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. 我有一个使用特殊字符的Sql statament(例如:('),(/),(&)),我不知道如何在我的VB.NET代码中编写它们。 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. 找出该字符的Unicode代码点(来自http://www.unicode.org ),然后使用ChrW将代码点转换为字符。 (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.) (把它放在另一个字符串中,使用连接。我有点惊讶VB没有转义序列,但我们去了。)

For example, for the Euro sign (U+20AC) you'd write: 例如,对于欧元符号(U + 20AC),您可以写:

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. 将字符直接放入源代码中的优势在于您的源代码保持“纯粹的ASCII” - 这意味着您不会遇到任何其他程序试图读取它,区分它等等的任何奇怪问题。当然,在代码中看到符号更难。

The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. 最常见的方式似乎是附加Chr(34)形式的字符... 34表示双引号字符。 The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap 字符代码可以从windows程序“charmap”中找到...只需要windows / Run ...并输入charmap

The ' character can be doubled up to allow it into a string eg '字符可以加倍,以允许它成为一个字符串,例如

lSQLSTatement = "Select * from temp where name = 'fred''s'" 

Will search for all records where name = fred's 将搜索name = fred的所有记录

If you are passing strings to be processed as SQL statement try doubling the characters for example. 如果要传递要作为SQL语句处理的字符串,请尝试将字符加倍。

"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "

The '' double works with the other special characters as well. ''double与其他特殊字符一起使用。

Three points: 三点:

1) The example characters you've given are not special characters. 1)您给出的示例字符不是特殊字符。 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: 2)要键入键盘上没有相应键的字符,请使用:

Alt + (the ASCII code number of the special character) Alt + (特殊字符的ASCII码编号)

For example, to type ¿, press Alt and key in 168 , which is the ASCII code for that special character. 例如,要键入¿,请按Alt并键入168 ,这是该特殊字符的ASCII代码。

You can use this method to type a special character in practically any program not just a VB.Net text editor. 您可以使用此方法在几乎任何程序中键入特殊字符,而不仅仅是VB.Net文本编辑器。

3) What you probably looking for is what is called 'escaping' characters in a string. 3)你可能正在寻找的是字符串中所谓的“转义”字符。 In your SQL query string, just place a \\ before each of those characters. 在SQL查询字符串中,只需在每个字符前放置\\ That should do. 那应该做。

Chr() is probably the most popular. Chr()可能是最受欢迎的。 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 如果要生成unicode字符,可以使用ChrW()ControlChars类包含一些特殊和“不可见”的字符,加上引号 - 例如,ControlChars.Quote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM