Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# vb.net max number in comma seperated string

Posted on 2016-08-31
Medium Priority
61 Views
I have a process that will build a string...

I need the ability to match a "set of numbers" against that
Pick out the highest number
And remove the lower numbers from the original string

Example
Original String  is "1,2,5,1002,1003,1004,1005"
Match Set  "1002,1003,1004"... in this 1004 is obviously the highest number

My revised original set is now "1,2,5,1004,1005"
0
Question by:lrbrister
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 64

Accepted Solution

Fernando Soto earned 1000 total points (awarded by participants)
ID: 41778839
Hi lrbrister;

This should do what you need.
``````'' Original Data
Dim original As String = "1,2,5,1002,1003,1004,1005"
Dim matchSet As String = "1002,1003,1004"
'' Hold Intermediary results
Dim originalList As New List(Of Integer)
Dim matchSetList As New List(Of Integer)
'' Remove the high value from the matchSet
matchSetList.Remove(matchSetList.Max())
'' Remove the values in originalList that remain in matchSetList
matchSetList.ForEach(Sub(i) originalList.Remove(i))
'' Turn the originalList to to a string
original = originalList.Aggregate("", Function(a, n) a & "," & Integer.Parse(n)).TrimStart(","c)
``````
0

LVL 30

Assisted Solution

anarki_jimbel earned 1000 total points (awarded by participants)
ID: 41778941
"Classic style" solution :)

``````Module Module1

Sub Main()
Dim s1 As String = "1,2,5,777,1002,1003,1004,1005"
Dim s2 As String = "17,1002,1003,1004"
Dim sResult As String = matchSets(s1, s2)
Console.WriteLine(sResult)
End Sub

Private Function matchSets(setString1 As String, setString2 As String) As String
Dim result As String = Nothing
Dim set1 As String() = setString1.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
Dim set2 As String() = setString2.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
' from the examples given we suggest that numbers are sorted
Dim nextIndex As Integer = 0
For j As Integer = 0 To set2.Length - 2 Step 1 ' loop on the matching set
For i = nextIndex To set1.Length - 1 Step 1 'loop on the set to be matched
If set2(j) = set1(i) Then
set1(i) = Nothing
nextIndex = i + 1
Exit For
End If
Next
Next
result = String.Join(",", set1)
While result.IndexOf(",,") > -1
result = result.Replace(",,", ",")
End While

Return result
End Function

End Module
``````
0

Author Comment

ID: 41779741
Hey guys...
I'll take a look as soon as I get back to my office
0

Author Comment

ID: 41800031
No objection to awarding as suggested.
0

LVL 64

Expert Comment

ID: 41806196
0

## Featured Post

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month10 days, 21 hours left to enroll