Android Question split error

yaniv hanya

Active Member
Licensed User
I have this code

B4X:
If name.Contains(".") Then
        Dim  tde() As String=Regex.Split(".",name)
        Return tde(tde.Length-1)
    End If
name is aname of a file.
for some resone i hav a aproblem in the regex line and the code crash in the return comnd with this log
Error occurred on line: 261 (ChooseFiles)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
at java.lang.reflect.Array.get(Array.java:170)
at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:584)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:383)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.choosefiles._getextension(choosefiles.java:758)
ted dont get initialized for some reason.
Why?
 

emexes

Well-Known Member
Licensed User
You could use the regular string functions too. Using regex here is like cracking nuts with a sledgehammer. Unless you're just warming up for some more complicated tasks, in which case I shall retreat back into my shell ;-)
B4X:
Dim CombinedName As String = "Monty.Python"
Dim DotPos As Int = (CombinedName & ".").IndexOf(".")
Dim FirstName As String = CombinedName.SubString2(0, DotPos)
Dim LastName As String = CombinedName.SubString(DotPos + 1)    'or just DotPos if you want dot too
   
Log(CombinedName)
Log(DotPos)
Log(FirstName)
Log(LastName)
B4X:
Monty.Python
5
Monty
Python
Always good to have a Plan B, though.
 

emexes

Well-Known Member
Licensed User
Realised later that you are splitting a filename. It might be that you are looking to split filename.extension into filename and extension, to determine the file type.You can do that with regex and non-greedy - or greedy, I can never remember - mode, but again is probably simpler just do with plain old String built-in methods:
B4X:
Dim CombinedName As String = "Monty Python.doc.zip"
Dim DotPos As Int = (CombinedName).LastIndexOf(".")
If DotPos < 0 Then
    Dim FileName As String = CombinedName
    Dim FileExtension As String = ""
Else
    Dim FileName As String = CombinedName.SubString2(0, DotPos)
    Dim FileExtension As String = CombinedName.SubString(DotPos + 1)    'or just DotPos if you want dot too
End If
   
Log(CombinedName)
Log(DotPos)
Log(FileName)
Log(FileExtension)
B4X:
Monty Python.doc.zip
16
Monty Python.doc
zip
 

emexes

Well-Known Member
Licensed User
actually, Erel's answer does the job well.
Agreed, regular expressions are great, but for anything past simple literal matches it gets complex quickly.
Why do you think it is better to use theString built-in functions
Huh? Better? That doesn't sound like me - I try to stay out of those philosophical wars. Let me check.

Righto, what I actually said was that using regex here was like cracking nuts with a sledgehammer. I'll stand by that. If anything, it's a positive statement about the power of regex.

Later I said it was probably simpler to use the built-in string methods. I'll stand by that too. If regex was simple, this thread would not exist.

My thinking was that if you are using this to split off the file extension to determine the type of the file, then splitting the full filename into more or fewer than 2 strings is probably not what you want.

Eg, for the example I used in post #6, for the file "Monty Python.doc.zip" then you probably want to know that it is a zip file, so that you can handball it to a decompression library.

Try feeding your app filenames like "noextension" and "lots.of.extensions.txt" and "dot.but.no.extension." and "" and "." and see what happens.
 
Last edited:

yaniv hanya

Active Member
Licensed User
Thanks
I come from the world of vb and ther split is part of the built-in string functions.
I really noticed that in "noextension" case it didn't return anything that was terrible.
in vb It at least returns the original string
 

emexes

Well-Known Member
Licensed User
I really noticed that in "noextension" case it didn't return anything that was terrible.
There was a quote in the old Turbo C manual about how C gives you plenty of rope to swing with... or hang yourself with. Regex is the same. C is one of my favourite languages, when applied correctly and where appropriate. Regex too.
in vb It at least returns the original string
as does my basic code using lowly string functions ;-)
 
Top