Solved

vb.net max number in comma seperated string

Posted on 2016-08-31
5
39 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)
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 62

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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