• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 62
  • Last Modified:

vb.net max number in comma seperated string

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
lrbrister
Asked:
lrbrister
  • 2
  • 2
2 Solutions
 
Fernando SotoCommented:
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)
original.Split(New Char() {","c}).ToList().ForEach(Sub(s) originalList.Add(Integer.Parse(s)))
matchSet.Split(New Char() {","c}).ToList().ForEach(Sub(s) matchSetList.Add(Integer.Parse(s)))
'' 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)

Open in new window

0
 
anarki_jimbelCommented:
"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)
        Console.ReadLine()
    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

Open in new window

0
 
lrbristerAuthor Commented:
Hey guys...
I'll take a look as soon as I get back to my office
0
 
lrbristerAuthor Commented:
Hey guys... I truly thought I had already split the points.
No objection to awarding as suggested.
0
 
Fernando SotoCommented:
Both solutions answered the question.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now