vb.net 你如何确定一个字符是否是来自 AZ 的字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/386495/
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
How do you determine if a Char is a Letter from A-Z?
提问by user38349
How do you determine if a letter is in the range from A-Z or Digit 0-9? We are getting some corrupted data "I_999?=???ae??òò?".
你如何确定一个字母是否在 AZ 或数字 0-9 的范围内?我们得到了一些损坏的数据“I_999?=???ae??òò?”。
I thought I could use Char.IsLetterOrDigit("?") to ID the corrupted data from "I_999?", but unexpectedly this is returning true. I need to trap this, any thoughts?
我以为我可以使用 Char.IsLetterOrDigit("?") 来从“I_999?”中识别损坏的数据,但出乎意料的是,这返回 true。我需要捕捉这个,有什么想法吗?
回答by Rob Williams
I can't help but notice that everyone seems to be missing the real issue: your data "corruption" appears to be an obvious character encoding problem. Therefore, no matter what you do with the data, you will be (mis)treating the symptom and ignoring the root cause.
我不禁注意到每个人似乎都忽略了真正的问题:您的数据“损坏”似乎是一个明显的字符编码问题。因此,无论您如何处理数据,您都将(错误)对待症状并忽略根本原因。
To be specific, you appear to be attempting to interpret the received binary BYTES as ASCII text, when those BYTES were almost-certainly intended to represent text encoded as something-other-than-ASCII.
具体来说,您似乎试图将接收到的二进制 BYTES 解释为 ASCII 文本,而这些 BYTES 几乎肯定是打算将文本编码为 ASCII 以外的内容。
You should find out what character encoding applies to the string of text that you received. Then you should read that data while applying the appropriate character encoding transformations.
您应该找出适用于您收到的文本字符串的字符编码。然后,您应该在应用适当的字符编码转换的同时读取该数据。
You should read Joel Spolsky's article that emphasizes that "There Ain't No Such Thing As Plain Text."
您应该阅读 Joel Spolsky 的文章,该文章强调“没有纯文本之类的东西”。
回答by EBGreen
Well there are two quick options. The first is to use a regular expression the second is to use the Asc() function to determine if the Ascii value is in the range of those allowable characters. I would personally use Asc() for this.
那么有两个快速选择。第一个是使用正则表达式,第二个是使用 Asc() 函数来确定 Ascii 值是否在这些允许字符的范围内。我个人会为此使用 Asc()。
回答by weiran
Should just be:
应该只是:
if (Regex.IsMatch(input, "[A-Za-z0-9]"))
{
// do you thang
}
回答by P Daddy
For Each m As Match In Regex.Matches("I_999?=???ae??òò?", "[^A-Z0-9]")
'' Found a bad character
Next
or
或者
For Each c As Char In "I_999?=???ae??òò?"
If Not (c >= "A"c AndAlso c <= "Z"c OrElse c >= "0"c AndAlso c <= "9"c) Then
'' Found a bad character
End If
Next
EDIT:
编辑:
Is there something wrongwith this answer that warrants the two anonymous downvotes? Speak up, and I'll fix it. I notice that I left out a "Then" (fixed now), but I intended this as pseudocode.
有什么不对这个答案权证两个匿名downvotes?说出来,我会解决的。我注意到我遗漏了“Then”(现在已修复),但我打算将其作为伪代码。
回答by Yuliy
You could use a regular expression to filter out the bad characters ... (use Regex.IsMatch instead if you only need to detect it)
您可以使用正则表达式来过滤掉坏字符......(如果您只需要检测它,请改用 Regex.IsMatch)
str = Regex.Replace(str, "[^A-Za-z0-9]","", RegexOptions.None);
回答by jinzai
The only way to ensure that you are dealing with printable ASCII characters, regardless of the encoding in the program or even in the string in question is to check each character for a legal value between 32 and 126 (127 = Delete -- not actually a 'printable' character).
确保您处理的是可打印的 ASCII 字符的唯一方法,不管程序中的编码,甚至是所讨论的字符串中的编码,是检查每个字符是否为 32 到 126 之间的合法值(127 = 删除——实际上不是一个“可打印”字符)。
i.e.
IE
Public Module StringExtensions
<Extension()>
Public Function IsASCII(inString As String, Optional bPrintableOnly As Boolean = True) ' 127 = Delete (non-printing) < 32 = control characters also, non-printing
Dim lowerLimit As Int32 = If(bPrintableOnly, 32, 0)
Dim upperLimit As Int32 = If(bPrintableOnly, 127, 128)
For Each ch In inString.ToCharArray()
If Not Asc(ch) < upperLimit OrElse Asc(ch) < lowerLimit Then
Return False
End If
Next
Return True
End Function
End Module
回答by Minh Hoàng
Use Asc(char) function. It returns a ANSI Character Code from 0 to 255. Check ANSI Character Codes Chart
使用 Asc(char) 函数。它返回一个从 0 到 255 的ANSI 字符代码。检查ANSI 字符代码图表
回答by Ghassen Arfaoui
Try the following code:
试试下面的代码:
NOT isNumeric(char)