Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Parse a string like "1-5,10,21,25-30"

  1. #1
    girldemsuga Guest

    Exclamation Parse a string like "1-5,10,21,25-30"

    I want to parse a string which like the following :

    1-5,10,21,25-30
    The end result should be a collection or list like:

    1
    2
    3
    4
    5
    10
    21
    25
    26
    27
    28
    29
    30
    Consider the string similar to what you would enter in the print dialog.

  2. #2
    Join Date
    Aug 2002
    Posts
    6,223
    Rep Power
    0

    Default

    You would need to do something like this:

    1. Start from beginning of string.
    2. Evaluate each character, if not a ",", then add to another string.
    3. If it is a ",", then evaluate new string already stored.
    4. Search for a "-" INSIDE the string, If none, output number. Goto 7.
    5. If exist then number before the "-" will be the start of a loop, after will be the end of the loop.
    6. Output numbers.
    7. Goto 2
    .
    PC - Ubuntu 15.04 64bit Desktop
    HP Pav G60-236US 3GB RAM Laptop, Ubuntu 15.04 64bit and Win7 Home

    "So Daddy, how come you telling me stealing not right when YOU copying DVDs? How come? How Come?"


    RIP Ramesh ...

  3. #3
    girldemsuga Guest

    Default

    I have a project I am working on which requires me to process strings in that form.
    I would love to beleive that I am not only person who has had to do something like this (apart from the people who create the print dialog).

    You know the saying, dont re-invent the wheel if it was already created.

  4. #4
    Join Date
    Aug 2002
    Posts
    6,223
    Rep Power
    0

    Default

    Believe it or not, the problem you have is not something unusual. Take a look at the Print Dialogue box for many MS Office applications. When you specify which pages you wish printed, you (the user) may type in that same exact string.
    .
    PC - Ubuntu 15.04 64bit Desktop
    HP Pav G60-236US 3GB RAM Laptop, Ubuntu 15.04 64bit and Win7 Home

    "So Daddy, how come you telling me stealing not right when YOU copying DVDs? How come? How Come?"


    RIP Ramesh ...

  5. #5
    girldemsuga Guest

    Thumbs up Parse in Range String

    Well people, not one has posted a solution so I went ahead and created it.
    So for those of you who are interested, the function can be found below.

    Code:
        ''' <summary>
        ''' Function used to break a ranged string apart into individual values which are the returned in a collection
        ''' If use this function, please leave this header in :)
        ''' </summary>
        ''' <param name="StringToParse">Ranged String to break apart eg. 1-5,10,21,25-30</param>
        ''' <author>Dredix</author>
        Private Function ParseRangeString(ByVal StringToParse As String) As Collection
            Dim split As String() = StringToParse.Split(",")    'Breaking the string apart by "," into array
            Dim vOutValues As New Collection                    'Declaring a new collection
            Dim vRaneCNT, vColLCNT As Integer                   'Used as counter
            Try     'Incase of error
                For Each s As String In split                   'Looping through array for values and range
                    If s.Trim() <> "" Then                      'Ensuring string is not empty
                        If InStr(s, "-") Then                   'Testing if string is a Range
                            For vRaneCNT = CInt(s.Substring(0, InStr(s, "-") - 1)) To CInt(s.Substring(InStr(s, "-")))  'Determine Range to loop
                                For vColLCNT = 1 To vOutValues.Count 'Ensuring the value is not already in the collection
                                    If CInt(vOutValues.Item(vColLCNT)) = CInt(vRaneCNT) Then GoTo NextRangeValue 'Jump over adding to collection
                                Next
                                vOutValues.Add(CInt(vRaneCNT))  'Adding value in Range to collection
    NextRangeValue:
                            Next
                        Else
                            For vColLCNT = 1 To vOutValues.Count 'Ensuring the value is not already in the collection
                                If CInt(vOutValues.Item(vColLCNT)) = CInt(s) Then GoTo NextValue 'Jump over adding to collection
                            Next
                            vOutValues.Add(CInt(s))             'Adding value to collection
    NextValue:
                        End If
                    End If
                Next s
                Return vOutValues                               'Returning Collection
            Catch ex As Exception
                Return vOutValues                               'Returning Collection
            End Try
        End Function
    Last edited by girldemsuga; Mar 12, 2010 at 02:10 PM. Reason: Add function header

  6. #6
    girldemsuga Guest

    Default

    Has this code been useful to anyone?

  7. #7
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    I've put this functionality in a script I made a while back. Something that would download selected episodes of a manga from a certain site. I don't know VB, but my code was much smaller than that was. All you needed to do was:

    1. Use the string split function to separate by ',' and put it in an array.
    2. Loop through the array (for loop) and write any strings not having a '-' (single number) or...
    3. ... Use another for loop to add each number in the string's specified range.

    I won't even try reading that segment of code . I hate VB for some unknown reason ...
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  8. #8
    girldemsuga Guest

    Default

    I run many error checks and ensure the same value is not inserted more than once.
    That may account for the larger code.

    I am interested in seeing what your version of this looks like.

  9. #9
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    ... so u made it more complicated that it needs to be... remember is a beginner requesting help
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  10. #10
    girldemsuga Guest

    Default

    Quote Originally Posted by icymint3 View Post
    ... so u made it more complicated that it needs to be... remember is a beginner requesting help
    I asked the question originally and no one posted a solution so I created a solution.
    Pardon me if its my style to check for errors. If someone needs a simplified solution, it could be striped down.
    Last edited by girldemsuga; Apr 14, 2010 at 07:46 PM.

Posting Permissions

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