Solved

vb.net max number in comma seperated string

Posted on 2016-08-31
5
30 Views
Last Modified: 2016-09-20
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
Comment
Question by:lrbrister
  • 2
  • 2
5 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points (awarded by participants)
Comment Utility
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
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points (awarded by participants)
Comment Utility
"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
 

Author Comment

by:lrbrister
Comment Utility
Hey guys...
I'll take a look as soon as I get back to my office
0
 

Author Comment

by:lrbrister
Comment Utility
Hey guys... I truly thought I had already split the points.
No objection to awarding as suggested.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Both solutions answered the question.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now