Solved

comparing Strings

Posted on 2006-10-24
6
248 Views
Last Modified: 2012-05-05
Hello All,
I need some help on where to start to compare ONE string to an array of strings and create ANOTHER ARRAY of matching strings following strings  they are both formated with a semicolon as a seperator BUT i want to compare the second string.  Here is an example
BTW they will be read in from an ascii file

String to use as what i want to compare everything against
on a note i won't know any comparison words.  Like for this example i wouldn't be able to put in wooden or door

WODOOR;wooden door

example of the different strings
10 02 01 ; solid wood door
10 03 04 ; hollow wood door
10 03 04 ; wooden laminent slider
10 25 04 ; window casement  <-- this should fail

I would want it to find the FIRST THREE examle BUT NOT THE LAST

Is their any hope??

Thanks
0
Comment
Question by:sherrick123
  • 3
  • 3
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17798314
What version of .Net, 2003 or 2005?
0
 

Author Comment

by:sherrick123
ID: 17798612
2005
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17798974
Hi sherrick123;

This solution uses Regular Expressions

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim TestData() As String = {"10 02 01 ; solid wood door", _
            "10 03 04 ; hollow wood door", "10 03 04 ; wooden laminent slider", _
            "10 25 04 ; window casement "}
        Dim FindWords As String = "WODOOR;wooden door"
        Dim MyFoundStrings() As String
        MyFoundStrings = DoesStringContain(FindWords, TestData)

    End Sub

    Private Function DoesStringContain(ByVal LookingFor As String, _
        ByVal LookingIn() As String) As String()

        Dim TempArray As New ArrayList

        LookingFor = LookingFor.Substring(LookingFor.IndexOf(";"c) + 1).Trim()
        LookingFor = Regex.Replace(LookingFor, "\s+", "`")
        LookingFor = Regex.Escape(LookingFor).Replace("`"c, "|")
        LookingFor = "\b" & Regex.Replace(LookingFor, "\s+", "|") & "\b"
        For Each str As String In LookingIn
            Dim ParseStr As String = str.Substring(str.IndexOf(";"c) + 1)
            If Regex.IsMatch(ParseStr, LookingFor, RegexOptions.IgnoreCase) Then
                TempArray.Add(str)
            End If
        Next

        Dim FoundStrings(TempArray.Count - 1) As String

        If TempArray.Count > 0 Then TempArray.CopyTo(FoundStrings)

        Return FoundStrings

    End Function
End Class


Fernando
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!

 

Author Comment

by:sherrick123
ID: 17799162
Thanks Fernando
0
 

Author Comment

by:sherrick123
ID: 17799183
Question though what do the switches mean like the \b \s ??
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17799198
They are Regular Expression meta-characters. The \b means that the next character start on a word boundary and the \s means a white space character.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 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