Solved

vb.net max number in comma seperated string

Posted on 2016-08-31
5
44 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
[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
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 250 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)
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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 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)
        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
ID: 41779741
Hey guys...
I'll take a look as soon as I get back to my office
0
 

Author Comment

by:lrbrister
ID: 41800031
Hey guys... I truly thought I had already split the points.
No objection to awarding as suggested.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41806196
Both solutions answered the question.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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