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
Solved

vb.net max number in comma seperated string

Posted on 2016-08-31
5
41 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 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 29

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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