R
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
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