I want to parse a string which like the following :
The end result should be a collection or list like:1-5,10,21,25-30
Consider the string similar to what you would enter in the print dialog.1
2
3
4
5
10
21
25
26
27
28
29
30
I want to parse a string which like the following :
The end result should be a collection or list like:1-5,10,21,25-30
Consider the string similar to what you would enter in the print dialog.1
2
3
4
5
10
21
25
26
27
28
29
30
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 ...
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.
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 ...
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
Has this code been useful to anyone?
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
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.
... 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