Solved

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

Posted on 2013-01-19
6
322 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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