Android Question split error

Discussion in 'Android Questions' started by yaniv hanya, Aug 1, 2019.

  1. yaniv hanya

    yaniv hanya Active Member Licensed User

    I have this code

    Code:
    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
    ted dont get initialized for some reason.
    Why?
     
  2. DonManfred

    DonManfred Expert Licensed User

    Post the full code. Or better a small project which shows the issue.
    you are trying to access index -1 of an empty array

    Post the name used in your code. I guess there is no dot. Maybe the problem is somewhere else.
     
    MarcoRome likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    The dot needs to be escaped:
    Code:
    Dim  tde() As String=Regex.Split("\.",name)
     
    Jorge M A and DonManfred like this.
  4. yaniv hanya

    yaniv hanya Active Member Licensed User

    Thenks.
    I come from the vb.net world and didn't think it is that way
     
  5. emexes

    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 ;-)
    Code:
    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)
    Code:
    Monty.Python
    5
    Monty
    Python
    Always good to have a Plan B, though.
     
    Shelby likes this.
  6. emexes

    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:
    Code:
    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)
    Code:
    Monty Python.doc.zip
    16
    Monty Python.doc
    zip
     
    Shelby likes this.
  7. yaniv hanya

    yaniv hanya Active Member Licensed User

    actually, Erel's answer
    does the job well.
    Why do you think it is better to use theString built-in functions
     
  8. emexes

    emexes Well-Known Member Licensed User

    Agreed, regular expressions are great, but for anything past simple literal matches it gets complex quickly.
    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: Aug 4, 2019
    Shelby likes this.
  9. yaniv hanya

    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
     
  10. emexes

    emexes Well-Known Member Licensed User

    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.
    as does my basic code using lowly string functions ;-)
     
    Shelby likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice