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

validate a link 2
Measuring a string of text 1
validate an url 4
dynamic meta tag 3
string.indexof case insensitive 4
set a date value 1
if request.querystring('Id") = "" 4
repeater control last row 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top