How do you determine if Char is a letter from AZ?

How do you determine if a letter is in the range of AZ or numbers 0-9? We receive some corrupted data "I_999Š = ÄÖÆaðøñòò".

I thought I could use Char.IsLetterOrDigit ("Š") to identify corrupted data from "I_999Š", but unexpectedly it returns true. Do I need to trap any thoughts?

+3
source share
8 answers

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 valid characters. I would personally use Asc () for this.

+5
source

, , , : "" . , , , () .

, BYTES ASCII, BYTES , -, -ASCII.

, , . , .

, , " " .

+12

:

if (Regex.IsMatch(input, "[A-Za-z0-9]"))
{
    // do you thang
}
+2
For Each m As Match In Regex.Matches("I_999Š=ÄÖÆaðøñòòñ", "[^A-Z0-9]")
    '' Found a bad character
Next

For Each c As Char In "I_999Š=ÄÖÆaðøñòòñ"
    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:

- , downvotes? , . , "" ( ), .

+1

... ( Regex.IsMatch, )

str = Regex.Replace(str, "[^A-Za-z0-9]","", RegexOptions.None);
+1

, ASCII, , - 32 126 (127 = - "" ).

..

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
+1

Use the Asc (char) function. It returns an ANSI character code from 0 to 255. Check the ANSI Character Codes Chart

0
source

Try using the following code:

NOT isNumeric(char)
0
source

All Articles