Results 1 to 5 of 5

Thread: [SOLVED] Multiple Spaces using Split String Method

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    185

    [SOLVED] Multiple Spaces using Split String Method

    Hi,
    I have an interesting task I am trying to figure out.
    I need to get a whole string even if it has one or two spaces

    Example:
    "Song of Solomon 2:1-5"
    "1 Peter 2:1-5"

    I need to be able to get "Song of Solomon" and "1 Peter" as a complete string.

    I understand the split somewhat (see code below) but I have never come accross a situation where there can be multiple spaces.

    In this code, I can split them out by "spaces", "-" and ":"'s
    See output below

    CODE:
    Code:
    Dim txt As String = "Song of Solomon 2:1-5"
    Dim strarr As String() = txt.Split(New Char() {" "c, "-"c, ":"c}, StringSplitOptions.RemoveEmptyEntries)
    
    For i As Integer = 0 To strarr.Length - 1
        Debug.Print(strarr(i))
    Next
    OUTPUT

    Song
    of
    Solomon
    2
    1
    10
    I can get the numbers I need fine. It's just "Book" strings that contains 1 or 2 spaces.
    So, how do I account for one or two paces in the "Book" portion of a string?
    Each string can be different.

    Any ideas are welcomed!
    Thanks
    Last edited by pixelink; Jul 26th, 2020 at 11:16 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    My Website
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,457

    Re: Multiple Spaces using Split String Method

    You wouldn't use Split for this. You'd use LastIndexOf to get the index of the last space and then Substring to get the substrings on either side of that.
    vb.net Code:
    1. Dim lastSpaceIndex = myString.LastIndexOf(" "c)
    2. Dim prefix = myString.Substring(0, lastSpaceIndex)
    3. Dim suffix = myString.Substring(lastSpaceIndex + 1)
    You can then split the suffix on colons and dashes if required.

  3. #3
    New Member
    Join Date
    Jul 2020
    Posts
    9

    Re: Multiple Spaces using Split String Method

    First, you don't know if the user might include more than single space. So you have replace all multiple spaces with single space.

    Code:
    Sub GetStr(str As String)
        Dim res As String
        Dim lastspace As Integer
        str = Regex.Replace(s, " {2,}", " ")
        lastspace = str.LastIndexOf(" ")
        res = str.SubString(0,lastspace)
        MsgBox (res)
    
    End Sub
    Last edited by dday9; Jul 26th, 2020 at 03:33 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    185

    Re: Multiple Spaces using Split String Method

    Quote Originally Posted by jmcilhinney View Post
    You wouldn't use Split for this. You'd use LastIndexOf to get the index of the last space and then Substring to get the substrings on either side of that.
    vb.net Code:
    1. Dim lastSpaceIndex = myString.LastIndexOf(" "c)
    2. Dim prefix = myString.Substring(0, lastSpaceIndex)
    3. Dim suffix = myString.Substring(lastSpaceIndex + 1)
    You can then split the suffix on colons and dashes if required.
    Would of never thought of that.

    I was able to return "Song of Solomon"

    I am still fleshing out the rest of the code.

    Thanks JMC for the tip

    @user0001 - Yes, I will look into this too because it would be a problem esspecially using the string to fetch records from a DB.

    Thanks for the tip user001
    Last edited by pixelink; Jul 26th, 2020 at 11:20 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    My Website
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    185

    Re: Multiple Spaces using Split String Method

    OKay... this is the fleshed out code that gives me what I need

    VB CODE (THAT WORKS)

    Code:
    Dim txt As String = "Song of Solomon 2:1-10"
    
    Dim lastSpaceIndex1 = txt.LastIndexOf(" "c)
    Dim prefix = txt.Substring(0, lastSpaceIndex1)
    Debug.Print(prefix)
    
    Dim lastSpaceIndex2 = txt.LastIndexOf(" "c)
    Dim suffix = txt.Substring(lastSpaceIndex2 + 1)
    Debug.Print(suffix)
    
    Debug.Print("------------------------")
    
    Dim strarr As String() = suffix.Split(New Char() {"-"c, ":"c}, StringSplitOptions.RemoveEmptyEntries)
    
    For i As Integer = 0 To strarr.Length - 1
      Debug.Print(strarr(i))
    Next

    OUTPUT
    Song of Solomon
    2:1-10
    ------------------------
    2
    1
    10
    This is prototype code.
    Now I can actually use it in my app and hook it up to my function

    Thanks
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    My Website
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

Posting Permissions

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



Click Here to Expand Forum to Full Width