[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# formatt number output with dash and comma

Posted on 2011-10-13
Medium Priority
514 Views
Last Modified: 2013-11-26
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)
0
Question by:wuanchu
• 2
4 Comments

LVL 9

Accepted Solution

chwong67 earned 1000 total points
ID: 36966544
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
``````
0

LVL 2

Assisted Solution

stuartmorris earned 1000 total points
ID: 36968041
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
``````
0

Author Comment

ID: 36969117
Thanks chwong67 and stuartmorris, your codes are great. Thanks again.
0

Author Comment

ID: 36969216
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
###### Suggested Courses
Course of the Month18 days, 11 hours left to enroll

#### 834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.