Solved

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

Posted on 2013-01-19
6
325 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

630 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