Solved

VB.Net  How to check a string against many other strings

Posted on 2013-01-19
6
320 Views
Last Modified: 2013-01-19
Hi Experts.   I have a simple string "abc" that must be matched against a list of about 350 other strings that are split  into about 20 groups.  Some strings within a group are always the same and will be hardcoded but some are dynamic and must be supplied by the user.
If a match is found then I need to increment a number by one to show that the string matched something in that group.

So  "abc" is matched against

Group One
hsg
usy


Group Two
osy
pau

Group Three
abc
ahz


And so group three would be incremented by 1

I though I could do "If Then ElseIf"  but with the user defined values it got lengthy and complicated.   Whats the correct way to do this??   Any help appreciated.
0
Comment
Question by:PNRT
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38796187
which framework ?
0
 
LVL 2

Author Comment

by:PNRT
ID: 38796205
Currently 2 and would prefer to stay there but could go to 4 if there was good reason
Thanks for the reply
0
 
LVL 9

Expert Comment

by:sognoct
ID: 38796443
I don't know how you define groups, so I can imagine it is some kind of object. I would define a group like this
public class groups 
public str as list(of string) = nothing 
public matched as int32 = 0 

Open in new window


then you can define a list of groups and search the string inside each group like this :
dim p as list(of groups) = new list(of groups)

now you can populate elements of the list and when you populate all of 350 groups
can search with a simple for each like this
For Each el As groups In groups
 if el.str.contains("abc") then el.matched +=1 
Next

Open in new window

0
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.

 
LVL 2

Author Comment

by:PNRT
ID: 38796513
Thanks for the help, this sounds exactly what I'm looking for but I'm struggling.   Firstly I had defined the groups into lists of strings and would like to keep this as it was quite lengthy

I created a new class with

Public Class Groups
    Public str As List(Of String) = Nothing
    Public matched As Int32 = 0
End Class

Then when I use

        Dim p As List(Of Groups) = New List(Of Groups)
        For Each el As Groups In Groups
            If el.str.Contains("abc") Then el.matched += 1
        Next

I get the error ......   Groups' is a type and cannot be used as an expression
0
 
LVL 9

Accepted Solution

by:
sognoct earned 500 total points
ID: 38796537
sorry I did not debug was just an example on the fly ... here is a working code

Public Class Groups
    Public str As New List(Of String)
    Public matched As Int32 = 0

    Public Sub New(ByVal str2split As String)
      For i As Int32 = 0 To (str2split.Length / 3) - 1
        str.Add(str2split.Substring(i * 3, 3))
      Next
    End Sub

  End Class

    Dim sp As New List(Of Groups)
    sp.Add(New Groups("hsgusytstaab"))
    sp.Add(New Groups("osypautfr"))
    sp.Add(New Groups("abcahz"))

    For Each el As Groups In sp
      If el.str.Contains("abc") Then
        el.matched += 1
      End If
    Next

Open in new window

0
 
LVL 2

Author Closing Comment

by:PNRT
ID: 38796552
Worked Great, many thanks
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

773 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