Android Question Illegal Characters in file name

stu14t

Active Member
Licensed User
Longtime User
Is there a way of preventing illegal file name characters within a EditText ?

When using as SaveFileDialog in VB it will catch illegal text automatically.
 

DonManfred

Expert
Licensed User
Longtime User
Search for a routine which checks a string whether it has only allowed characters or not. I´m sure you´ll find an example here.
That you can use in focus_change or when clicking a button or such sutuation.
 
Upvote 0

stu14t

Active Member
Licensed User
Longtime User
I've found this and I'll have a go at converting. Doesn't look too challenging:

B4X:
Function CleanInput(strIn As String) As String
        ' Replace invalid characters with empty strings.
        Try
          Return Regex.Replace(strIn, "[^\w\.@-]", "")
        ' If we timeout when replacing invalid characters, 
        ' we should return String.Empty.
        Catch e As RegexMatchTimeoutException
          Return String.Empty       
        End Try
    End Function
 
Upvote 0

stu14t

Active Member
Licensed User
Longtime User
Ideal, Thanks NJDude

I'll convert that into a function :D

B4X:
Dim BadChars As String
Dim Text As String
Dim Matcher1 As Matcher

BadChars = "[.,+*;]" 'Add the unwanted characters here between the square brackets
          
Text = "Some, list + of words; some not wanted like * for example."
          
Matcher1 = Regex.Matcher(BadChars, Text)
          
Do While Matcher1.Find
      
  buffer = Matcher1.Match
  Text = Text.Replace(buffer, "")

Loop
          
Msgbox("Done: " & Text, "")

I take it "buffer" is a string, as it appears to be undeclared?
 
Last edited:
Upvote 0

stu14t

Active Member
Licensed User
Longtime User
I've used this code and altered some of the illegal characters in the string but I get an error:
B4X:
Sub CheckValid(StringToCheck As String) As String
    Dim BadChars As String
    Dim buffer As String
    Dim Matcher1 As Matcher

    BadChars = "[.,+*;\]" 'Add the unwanted characters here between the square brackets
                   
    Matcher1 = Regex.Matcher(BadChars, StringToCheck)
             
    Do While Matcher1.Find
         
      buffer = Matcher1.Match
      StringToCheck = StringToCheck.Replace(buffer, " ")

    Loop
   
    Return StringToCheck


End Sub

This is the error i'm getting:

B4X:
java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 8:
[.,+*;\]
        ^
    at java.util.regex.Pattern.compileImpl(Native Method)
    at java.util.regex.Pattern.compile(Pattern.java:411)
    at java.util.regex.Pattern.<init>(Pattern.java:394)
    at java.util.regex.Pattern.compile(Pattern.java:374)
    at anywheresoftware.b4a.keywords.Regex.getPattern(Regex.java:34)
    at anywheresoftware.b4a.keywords.Regex.Matcher2(Regex.java:97)
    at anywheresoftware.b4a.keywords.Regex.Matcher(Regex.java:90)
    at dcp.data_2014.main._checkvalid(main.java:1021)
    at dcp.data_2014.main._txtlocation_enterpressed(main.java:3138)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:161)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:157)
    at anywheresoftware.b4a.objects.EditTextWrapper$2.onEditorAction(EditTextWrapper.java:107)
    at android.widget.TextView.onEditorAction(TextView.java:4236)
    at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5081)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

I'm not sure exact;y what the issue is, the order or the characters?
 
Upvote 0

stu14t

Active Member
Licensed User
Longtime User
Brilliant!

Thanks for the prompt reply.

Does that also apply to / forward slash too? I just want to trap characters that will mess up a file name.
 
Upvote 0
Top