vb.net 字符看起来像 ASCII 63 但不是所以我不能删除它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25838639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:13:06  来源:igfitidea点击:

Character looks like ASCII 63 but isn't so I can't remove it

vb.netasciinon-ascii-characters

提问by Lou

I'm reading text from a text file. The first string the text file has to read is "Algood?", and note the space. In Notepad, it appears that there is a space in this string, but it isn't. When I test the 6th (zero-based index) character in Visual Studio's QuickWatch, it appears as:

我正在从文本文件中读取文本。文本文件必须读取的第一个字符串是“Algood?”,并注意空格。在记事本中,这个字符串中似乎有一个空格,但实际上没有。当我在 Visual Studio 的 QuickWatch 中测试第 6 个(从零开始的索引)字符时,它显示为:

"?"c

When I use the Ascfunction to get the ASCII code, it tells me that the ASCII code is 63. 63 is a question mark. But when I test to see if the string contains ASCII 63, it tests false. So it appears that the string contains the character with the ASCII code 63, only it doesn't, it contains some other character which tests as ASCII code 63. This is a problem: I can't remove the character if I don't know what to call it. I could remove the last character, but not every string in the text file contains this character.

当我使用该Asc函数获取ASCII码时,它告诉我ASCII码是63。63是一个问号。但是当我测试字符串是否包含 ASCII 63 时,它测试为 false。因此,该字符串似乎包含带有 ASCII 代码 63 的字符,只是它不包含,它包含一些其他字符,这些字符测试为 ASCII 代码 63。这是一个问题:如果我不删除,则无法删除该字符知道怎么称呼它。我可以删除最后一个字符,但并非文本文件中的每个字符串都包含该字符。

enter image description here

在此处输入图片说明

The question is: what is this character if not a question mark, and how can I uniquely identify so I can remove it?

问题是:如果不是问号,这个字符是什么,我如何唯一标识以便删除它?

回答by Hans Passant

It is the Unicode replacement character, U+FFFD, aka ChrW(&HFFFD).

它是Unicode 替换字符,U+FFFD,又名ChrW(&HFFFD)

Never use Asc() or Chr(), they are legacy VB6 functions that do not handle Unicode. Passing a fancy Unicode codepoint to Asc() always produces 63, the character code for "?"c, aka "I have no idea what you're saying". The exactsame idea as"?"cbut using an ASCII code instead.

切勿使用 Asc() 或 Chr(),它们是不处理 Unicode 的遗留 VB6 函数。将花哨的 Unicode 代码点传递给 Asc() 总是会产生 63,即 的字符代码"?"c,也就是“我不知道你在说什么”。在精确的原理相同"?"c,但使用的ASCII码代替。

Seeing the Black Diamond of Death back is always bad news, something went wrong when the string was converted from the underlying byte values. Because some byte values did not produce a valid character. Which is what you really should be looking for, you always want to avoid GIGO. Garbage In Garbage Out is an ugly data corruption problem that has no winners, only victims. You.

看到死亡黑钻石回来总是坏消息,当字符串从底层字节值转换时出现问题。因为某些字节值没有产生有效字符。这才是你真正应该寻找的,你总是想避开 GIGO。Garbage In Garbage Out 是一个丑陋的数据损坏问题,没有赢家,只有受害者。你。

回答by Ming Yuen

I have wrote the following function in Excel VBA which will remove the "black diamond" for a single cell.

我在 Excel VBA 中编写了以下函数,它将删除单个单元格的“黑色菱形”。

The hardest thing is to not loop each digit in all field to find it. I needed a method to identify the black diamond without check all digits of all fields.

最难的是不要循环所有字段中的每个数字来找到它。我需要一种无需检查所有字段的所有数字即可识别黑色菱形的方法。

I used a ADODB recordset, if the string is not accepted by the RS, it means it contains an invalid character. Then it looks for a ASC(63) = “?”, then it trims the cell down to without the black diamond.

我使用了 ADODB 记录集,如果 RS 不接受该字符串,则表示它包含无效字符。然后它查找 ASC(63) = “?”,然后将单元格修剪成没有黑色菱形。

The reason this work is when it loops through each digit in the string, it will recognize the black diamond as ASC = 63. If is a real question mark, it will be accepted by the RS.

这项工作的原因是当它循环遍历字符串中的每个数字时,它会将黑色菱形识别为 ASC = 63。如果是真正的问号,它将被 RS 接受。

Private Function Correct_Black_Diamond(ByVal First_Address As Variant) As String
    Dim CheckDigit As Integer
    Dim Temp_string As String
    Dim temp_Rs As New ADODB.Recordset
        temp_Rs.Fields.Append "address", adChar, 9999
        temp_Rs.Open

        temp_Rs.AddNew
            On Error GoTo Further_Address_Check
            temp_Rs!Address = First_Address
        temp_Rs.Update

        Correct_Black_Diamond = First_Address
    Exit Function

Further_Address_Check:
        For CheckDigit = 1 To Len(First_Address)
            If Asc(Mid(First_Address, CheckDigit, 1)) = 63 Then
                Temp_string = Trim(Mid(First_Address, 1, CheckDigit - 1)) & Trim(Mid(First_Address, CheckDigit + 1, Len(First_Address)))
            End If
        Next CheckDigit
        First_Address = Temp_string
        Correct_Black_Diamond = First_Address
        Exit Function

End Function

回答by Syed Nooruddin Kaleem Qadri

Use:

用:

LDM_MSG.Replace(ChrW(8203), "") 

Instead of:

代替:

LDM_MSG.Replace(Chr(63), "")

It solves the problem.

它解决了这个问题。