B4J Question Detect if ASCII

Blueforcer

Well-Known Member
Licensed User
Longtime User
i woul like to detect if a String only contains ASCII characters. Is this possible?
So for example i can sort out chinese strings.
 
Last edited:

Sandman

Expert
Licensed User
Longtime User
This isn't a speciality of mine so I'd be hesitant to recommend a rock-solid approach. But I will say that your description is a bit vague:

detect if a String only contains ASCII characters
That's one question.

sort out chinese strings
That's another question.

Follow-up question: How you are planning to deal with Indian characters? Russian? Thai? Emojis? Are they all allowed to stay because you only want to filter away the chinese chars?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you mean this?

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim t() As Byte
    t="dé´údfd乾卦彖傳應作「大明終始」".GetBytes("UTF8")
    For x=0 To t.Length-1
        If t(x)>127 Or t(x)<0 Then t(x)=127
    Next
Log(BytesToString(t,0,t.Length,"UTF8"))
End Sub

ddfd
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X:
    Dim string1 = "Hello World"
    Dim string2 = "dé´údfd乾卦彖傳應作「大明終始」"
    Dim string3 = "終始」"
    Dim string4 = ""
    
    Log(Regex.IsMatch("[\x00-\x7F]*", string1))
    Log(Regex.IsMatch("[\x00-\x7F]*", string2))
    Log(Regex.IsMatch("[\x00-\x7F]*", string3))
    Log(Regex.IsMatch("[\x00-\x7F]*", string4))

Result:
true
false
false
true
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim string1 = "Hello World"
    Dim string2 = "dé´údfd乾卦彖傳應作「大明終始」"
    Dim string3 = "終始」"
    Dim string4 = ""
   
    Log(Regex.IsMatch("[\x00-\x7F]*", string1))
    Log(Regex.IsMatch("[\x00-\x7F]*", string2))
    Log(Regex.IsMatch("[\x00-\x7F]*", string3))
    Log(Regex.IsMatch("[\x00-\x7F]*", string4))

Result:
Works perfectly. Thanks!
 
Upvote 0
Top