Home All Groups Group Topic Archive Search About

search for a string or part of a string in another string

Author
17 Apr 2010 8:12 AM
Russ Green
I'm trying to write some code to search for occurrences of a string in
another.

I have a form with a text box where a user can input text to search for. The
app then files all file names containing that string and loads into a
listview.  Simples!

except I want to be able to find exact matches or parts of the string.
Specifically I think I've got to the point where I have 2 string arrays. I
want to check if all strings in array1 exist in array2.  array2 can contain
more strings than array1 so just checking if they are equal won't work.

Thanks

Russ

    Private Function SearchWithinString(ByVal Search As String, ByVal Source
As String) As Boolean
        Dim retval As Boolean = False

        Dim m_Separators() As Char = {" "c}
        Dim m_SourceWords() As String
        Dim m_SearchWords() As String

        Dim m_Search As String = Search.ToLower
        Dim m_Source As String = Source.ToLower

        If m_Search.StartsWith("""") = True And m_Search.EndsWith("""") =
True Then
            'we must want to match the exact string only
            Dim trimarray() As Char = {""""c}
            If
m_Source.ToLower.Contains(m_Search.ToLower.TrimEnd(trimarray).TrimStart(trimarray))
= True Then retval = True

        ElseIf m_Search.Contains(" + ") = True Then
            'i think we have a situation where words MUST all exist,
            'though not necessarily in order

            'split the source into words
            m_SourceWords = m_Source.Split(m_Separators)
            m_SearchWords = m_Search.Split(m_Separators)

            'Code to strip empty words omitted.

            'How do i find if ALL strings in m_SearchWords() exist in
m_SourceWords()?

        Else
            'we just want to find if at least one or more of our search
words exist in the source text.

            'split the source into words
            m_SourceWords = m_Source.Split(m_Separators)
            m_SearchWords = m_Search.Split(m_Separators)

            'Code to strip empty words omitted.

             Do While retval = False
                For Each Word As String In m_SearchWords
                    If Source.Contains(Word) Then retval = True
                Next
            Loop
        End If

        Return retval
    End Function

Author
17 Apr 2010 12:11 PM
Cor Ligthert[MVP]
Hi Russ,

For this kind of complex string comparing is Regex build.

I'm not a fan from it, but sometimes splitting, searching etc becomes to
much spaghetti

Take a search with a search enginge, this is the first which I got
http://www.regular-expressions.info/tutorial.html

Cor



Show quoteHide quote
"Russ Green" <russ.gr***@live.co.uk> wrote in message
news:9D6CF3EC-AD35-46D6-AA94-0F9DA621E571@microsoft.com...
> I'm trying to write some code to search for occurrences of a string in
> another.
>
> I have a form with a text box where a user can input text to search for.
> The app then files all file names containing that string and loads into a
> listview.  Simples!
>
> except I want to be able to find exact matches or parts of the string.
> Specifically I think I've got to the point where I have 2 string arrays. I
> want to check if all strings in array1 exist in array2.  array2 can
> contain more strings than array1 so just checking if they are equal won't
> work.
>
> Thanks
>
> Russ
>
>    Private Function SearchWithinString(ByVal Search As String, ByVal
> Source As String) As Boolean
>        Dim retval As Boolean = False
>
>        Dim m_Separators() As Char = {" "c}
>        Dim m_SourceWords() As String
>        Dim m_SearchWords() As String
>
>        Dim m_Search As String = Search.ToLower
>        Dim m_Source As String = Source.ToLower
>
>        If m_Search.StartsWith("""") = True And m_Search.EndsWith("""") =
> True Then
>            'we must want to match the exact string only
>            Dim trimarray() As Char = {""""c}
>            If
> m_Source.ToLower.Contains(m_Search.ToLower.TrimEnd(trimarray).TrimStart(trimarray))
> = True Then retval = True
>
>        ElseIf m_Search.Contains(" + ") = True Then
>            'i think we have a situation where words MUST all exist,
>            'though not necessarily in order
>
>            'split the source into words
>            m_SourceWords = m_Source.Split(m_Separators)
>            m_SearchWords = m_Search.Split(m_Separators)
>
>            'Code to strip empty words omitted.
>
>            'How do i find if ALL strings in m_SearchWords() exist in
> m_SourceWords()?
>
>        Else
>            'we just want to find if at least one or more of our search
> words exist in the source text.
>
>            'split the source into words
>            m_SourceWords = m_Source.Split(m_Separators)
>            m_SearchWords = m_Search.Split(m_Separators)
>
>            'Code to strip empty words omitted.
>
>             Do While retval = False
>                For Each Word As String In m_SearchWords
>                    If Source.Contains(Word) Then retval = True
>                Next
>            Loop
>        End If
>
>        Return retval
>    End Function
>