formatt number output with dash and comma

A list of input numbers seperated by comma, such as  101,102,103,180,181,200,210,211,212,213,  I want to simply output as 101-103,180,181, 200,210-213that 3 or more continous number will be connect by dash such as 101,102,103, output as 101-103, others will be output with comma seperated even 2 continous numbers.

Please help me by vb.net(2008)
wuanchuAsked:
Who is Participating?
 
chwong67Connect With a Mentor Commented:
try function below:
Function getstring(ByVal StrA As String)

        Dim StrB As String
        Dim Str() As String = stra.Split(",")
        Dim StrEnd As String
        Dim StrStart As String

        For i As Integer = 0 To Str.Length - 1
            If i = 0 Then
                StrB = Str(i)
                StrStart = Str(i)
                StrEnd = ""
            ElseIf Str(i) - 1 = Str(i - 1) Then
                StrEnd = Str(i)
            Else
                If StrEnd <> "" Then
                    If StrEnd - 1 = StrStart Then
                        StrB = StrB & "," & StrEnd
                    Else
                        StrB = StrB & "-" & StrEnd
                    End If
                End If
                StrB = StrB & "," & Str(i)
                StrStart = Str(i)
                StrEnd = ""
            End If
        Next
        If StrEnd <> "" Then
            If StrEnd - 1 = StrStart Then
                StrB = StrB & "," & StrEnd
            Else
                StrB = StrB & "-" & StrEnd
            End If
        End If
        Return StrB
    End Function

Open in new window

0
 
stuartmorrisConnect With a Mentor Commented:
Tested this successfully with your input values.


Dim inputList As LinkedList(Of Integer) = New LinkedList(Of Integer)
        Dim inputArray = TextBox1.Text.Split(",")
        Dim rootNode = inputList.AddFirst(Integer.Parse(inputArray(0)))
        Dim previousNode
        For i As Integer = 1 To inputArray.Length - 1
            If i = 1 Then
                previousNode = inputList.AddAfter(rootNode, inputArray(i))
            Else
                previousNode = inputList.AddAfter(previousNode, inputArray(i))
            End If
        Next
        Dim currentNode = rootNode
        Dim pointerNode = currentNode
        Dim consecutiveCounter As Integer = 0
        Dim moveOn As Boolean = False
        Dim outputString As String = ""
        Dim delimiter As String = ""
        While Not currentNode Is Nothing
            previousNode = currentNode
            pointerNode = currentNode.Next()
            consecutiveCounter = 0
            If Not pointerNode Is Nothing Then
                While Not pointerNode Is Nothing And moveOn = False
                    If previousNode.Value() + 1 = pointerNode.Value() Then
                        consecutiveCounter = consecutiveCounter + 1
                        previousNode = pointerNode
                        pointerNode = pointerNode.Next()
                    Else
                        moveOn = True
                    End If
                End While
                If pointerNode Is Nothing Then moveOn = True
                If consecutiveCounter >= 2 Then
                    outputString = outputString & delimiter & currentNode.Value() & "-" & previousNode.value()
                    delimiter = ","
                    If moveOn Then
                        moveOn = False
                        currentNode = pointerNode
                        Continue While
                    End If
                Else
                    outputString = outputString & delimiter & currentNode.Value()
                End If
                moveOn = False
                consecutiveCounter = 0
                delimiter = ","
                
            End If
            currentNode = currentNode.Next()
        End While
        TextBox2.Text = outputString

Open in new window

0
 
wuanchuAuthor Commented:
Thanks chwong67 and stuartmorris, your codes are great. Thanks again.
0
 
wuanchuAuthor Commented:
hi stuartmorris sorry for the points .While I click the accept the solution, there is no your name list in the page and did not average the points to you. Usually there are some button options to select, but I clicked the accep the solution ,there is no buttion options. I am very sorry for that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.