ascii or not, the charset of a string

  • Thread starter Thread starter Guoqi Zheng
  • Start date Start date
G

Guoqi Zheng

How can I know the charset of string? I only need to know a string contains
only ascii characters or it contains more than just ascii, maybe some
chinese characters. how can I do this?

--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.com
 
Would something like this work for you:

Public Function IsAscii(ByVal str As String) As Boolean
Dim chars() As Char = str.ToCharArray()
For Each c As Char In chars
If c > Chr(127) Then
Return False
End If
Next
Return True
End Function

Karl
 
Guoqi Zheng said:
How can I know the charset of string? I only need to know a string contains
only ascii characters or it contains more than just ascii, maybe some
chinese characters. how can I do this?

Hi Zheng,

One way is to use AscW function. It returns an integer that
represents the Unicode value of a passed chararacter.


TTH,
Adel A Al-saleh
 
I used below, do you it is reasonable?

Private Function isAscii(ByVal strText As String) As Boolean

Dim oByte As [Byte]() = System.Text.Encoding.ASCII.GetBytes(strText)

Dim strBack As String = System.Text.Encoding.ASCII.GetString(oByte)

If strBack = strText Then

Return True

Else

Return False

End If

End Function


--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.com
 
I think that would work...ASCII.GetString() will replace any non-ascii
characters with "?", so your strings wouldn't be equal (which is what you
want).

Karl

Guoqi Zheng said:
I used below, do you it is reasonable?

Private Function isAscii(ByVal strText As String) As Boolean

Dim oByte As [Byte]() = System.Text.Encoding.ASCII.GetBytes(strText)

Dim strBack As String = System.Text.Encoding.ASCII.GetString(oByte)

If strBack = strText Then

Return True

Else

Return False

End If

End Function


--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.com

Karl said:
Would something like this work for you:

Public Function IsAscii(ByVal str As String) As Boolean
Dim chars() As Char = str.ToCharArray()
For Each c As Char In chars
If c > Chr(127) Then
Return False
End If
Next
Return True
End Function

Karl
 
Back
Top