VB.NET How to strip unwanted values from a string?

Hi guys, I need to do some string manipulation but not really sure how to go about it.

I have a string that is read from a database that typically looks like the below:
Windows Server 2003, Windows Server 2008
Windows 7
Windows Server 2003, junk, more junk, Windows Server 2008, junk, Windows Server 2012
Windows 7, junk, Windows 8

What I'm trying to do is to have it look at the values between the commas and remove it if it doesn't equal one of the below:
Windows Server 2003
Windows Server 2008
Windows Server 2008 R2
Windows Server 2012
Windows Server 2012 R2
Windows 7
Windows 8

Below are some examples of what I'm trying to achieve.

Original string
Windows Server 2003, junk, more junk, Windows Server 2008, junk, Windows Server 2012

Clean string
Windows Server 2003, Windows Server 2008, Windows Server 2012

Original string
Windows 7, junk

Clean string
Windows 7

So "junk" is anything that doesn't equal one of those operating systems listed above.

How would I go about doing this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Define the following array:
''define array of acceptable values
Dim vals as string()={"Windows Server 2003","Windows Server 2008","Windows Server 2008 R2","Windows Server 2012","Windows Server 2012 R2","Windows 7","Windows 8"}

Open in new window

Then copy this function:

''this function checks if a particular value matches one of the values in the array
Public Function EqualsTo(ByVal str As String, ByVal ParamArray values As String()) As Boolean
        For Each value In values
            If str.Equals(value, System.StringComparison.InvariantCultureIgnoreCase) Then
                Return True
            End If
        Return False
    End Function

Open in new window

Then call it as:

''split your string on a comma to create an array of values from between the commas

''check if array contains any values
if arr.Length > 0 then
''loop through each value in an array and check if it matches one of the values in array of acceptable values
for each i in arr.Length-1
If EqualsTo(arr(i), vals) Then
  ... do whatever here
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Another example:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim raw As String = "Windows Server 2003, junk, more junk, Windows Server 2008, junk, Windows Server 2012" ' <-- data from your DB
        raw = CleanOperatingSystems(raw)
    End Sub

    Private Function CleanOperatingSystems(ByVal RawString As String) As String
        Static AllowedSystems() As String = {"Windows Server 2003", "Windows Server 2008",
                                             "Windows Server 2008 R2", "Windows Server 2012",
                                             "Windows Server 2012 R2", "Windows 7", "Windows 8"}

        Dim RawValues As New List(Of String)(RawString.Split(","))
        For i As Integer = RawValues.Count - 1 To 0 Step -1
            RawValues(i) = RawValues(i).Trim
            If Array.IndexOf(AllowedSystems, RawValues(i)) = -1 Then
            End If
        Return String.Join(", ", RawValues.ToArray)
    End Function

Open in new window

amaru96Author Commented:
Thanks guys, I can work with those.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.