Inserting Asterisk between Number Ranges

suresh_ed

Active member
Joined
May 19, 2008
Messages
27
Programming Experience
1-3
str1="1,3,4,5,9,10,11,13,14"

I have to check number ranges. for example if i found 3,4,5 if should insert
*** between first number and last number.

Not to do:

If i found 13,14 i should not insert ***. Range count s/b greater than 2.

Output should be
output = "1,3***5,9***11,13,14"


Regards,
Suresh
 
Thanks for the exercise ;) My approach is to define a Range structure that hold the first and last number in range. Then add method to parse the input string into array of ranges, and methods that outputs a range and an array of ranges to string. Unfortunately for you I no longer have VB2003 (nor VB2005) installed, only VB2008, but perhaps you could get some ideas from this code still, so I decide to post it. If you choose to go for a direct translation to the old .Net then I can see you have use ArrayList instead of the List(Of T) in ParseRanges method, and in RangesToString method you have to do a loop instead of the ConvertAll call, other than that I can't immediately see other version issues.
VB.NET:
Public Structure Range

    Private _first As Integer
    Public Property First() As Integer
        Get
            Return _first
        End Get
        Set(ByVal value As Integer)
            _first = value
        End Set
    End Property

    Private _last As Integer
    Public Property Last() As Integer
        Get
            Return _last
        End Get
        Set(ByVal value As Integer)
            _last = value
        End Set
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return Last - First + 1
        End Get
    End Property

    Private _delim As String
    Public Property RangeDelimiter() As String
        Get
            If _delim Is Nothing Then
                Return "-"
            Else
                Return _delim
            End If
        End Get
        Set(ByVal value As String)
            _delim = value
        End Set
    End Property

    Public Sub New(ByVal first As Integer, ByVal last As Integer, Optional ByVal rangeDelimiter As String = "-")
        Me.First = first
        Me.Last = last
        Me.RangeDelimiter = rangeDelimiter
    End Sub

    Public Shared Function ParseRanges(ByVal input As String, Optional ByVal rangeDelimiter As String = "-") As Range()
        Dim numbers As New List(Of Integer)
        For Each m As Match In Regex.Matches(input, "\d+")
            numbers.Add(CInt(m.Value))
        Next
        numbers.Sort()
        Dim ranges As New List(Of Range)
        Dim r As New Range(numbers(0), numbers(0), rangeDelimiter)            
        For i As Integer = 1 To numbers.Count - 1
            If numbers(i) - numbers(i - 1) = 1 Then
                r.Last = numbers(i)
            Else
                ranges.Add(r)
                r = New Range(numbers(i), numbers(i), rangeDelimiter)
            End If
            If i = numbers.Count - 1 Then
                ranges.Add(r)
            End If
        Next
        Return ranges.ToArray
    End Function

    Public Overrides Function ToString() As String
        Select Case Me.Count
            Case 1
                Return Me.First.ToString
            Case 2
                Return String.Format("{0},{1}", Me.First, Me.Last)
            Case Else
                Return String.Format("{0}{1}{2}", Me.First, Me.RangeDelimiter, Me.Last)
        End Select
    End Function

    Public Shared Function RangesToString(ByVal ranges() As Range) As String
        Dim rs() As String = Array.ConvertAll(ranges, Function(r As Range) r.ToString)
        Return String.Join(",", rs)
    End Function

End Structure
sample calls:
VB.NET:
Dim input As String = "1,3,4,5,6,8,10,11,12;100;101;102*103"
Dim rangestring As String = Range.RangesToString(Range.ParseRanges(input, "***"))
'result: 1,3***6,8,10***12,100***103
 
Inserting Asterisk between ranges

Hello JohnH,

It's is working perfectly. Sorry for the late response last two days was holiday for me.

List(Of T) & ConvertAll method both are working fine in VS 2005 also.

Regards,
Suresh
 
Yes, they do, but VB 2005 doesn't have the Function lambda expression, you have use AddressOf to a full method. Perhaps you should change your profile from VS 2003 to VS 2005 ?
 
Back
Top