Solved

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

Posted on 2013-01-19
6
317 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
Comment Utility
which framework ?
0
 
LVL 1

Author Comment

by:PNRT
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:PNRT
Comment Utility
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
Comment Utility
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 1

Author Closing Comment

by:PNRT
Comment Utility
Worked Great, many thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now