Consulting

Results 1 to 5 of 5

Thread: split counting

  1. #1
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location

    split counting

    Hi to everyone . I have a string like "String=Stri1-Stri2-Stri3" . I use the split (String,"-") and i have 3 strings, now, is there any method to get the number (for the example the number is 3) of the substrings from the split command?
    Regards Kostas

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    7,343
    Location
    Option Explicit
    
    
    Sub test()
        Dim s As String
        Dim v As Variant
        
        s = "Stri1-Stri2-Stri3"
        v = Split(s, "-")
        
        'sometimes people have Option Base 1 set, so this is safest way
        MsgBox UBound(v) - LBound(v) + 1
        
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Guru
    Joined
    Apr 2012
    Posts
    4,929
    Sub M_snb()
       msgbox ubound(split("String=Stri1-Stri2-Stri3","-"))+1
    End Sub

  4. #4
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location
    Thank you very much for your answers

  5. #5
    VBAX Expert
    Joined
    Apr 2005
    Posts
    821
    Location
    What doe you expect the answer to be if you have a string like "Stri7-Stri8-Stri9"? 3 or 9?
    I understand it different.
    Dim str As String
    str = "Stri1-Stri2-Stri3"
    MsgBox (Mid(Split(str, "-")(2), 5))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •