ascii or not, the charset of a string

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
 
K

Karl

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
 
A

Adel Al-saleh

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
 
G

Guoqi Zheng

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
 
K

Karl

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

string.indexof case insensitive 3
shift + ctrl charactor 2
dynamic meta tag 2
. in regular expression 3
validate an url 4
set a date value 1
Html to text converter 1
where to look for the error 2

Top