-
March 20th, 2013, 03:04 PM
#1
Quuikest way to determine if an array element exists in a string
I've been looking for most of the day so far and can find a plethora of ways to determine if a string is present in an array element but that's not what I'm looking for.
I have a string array that contains authors names, about 175,000 of them.
I have a string that contains (possibly) a book title, series and author name - though not necessarily in that order.
Currently I'm taking the brute force approach (code follows), but there has to be a better way and I'm too involved with the results to see it. Since I'm constantly looking at this array in my code, I'm spending almost 35% of my time in [b]this[b] code so any improvement would be beneficial.
Code:
Public Function FindAuthors(ByVal file As String) As String
' Getting the same name twice (or more?) possibly if the name appears in
' different forms on the same title? -- Fix 1
If file = "" Then Return ""
If file.Substring(0, 1) = "*" Then Return file ' if the author is an "*" then it's a special case possibly no author, or a magazine ...
Dim loc As Integer
Dim strTemp As String = CleanInput(file) ' regex remove all except alphanumeric and white space
Dim strTempAuthor As String = Nothing
Dim myOptions As StringComparison = StringComparison.CurrentCultureIgnoreCase
' TODO: need to be able to eliminate names which are partials, eve adam <-> steve adams
Dim strAuthorsOut As String = Nothing
For Each kvp As KeyValuePair(Of String, Integer) In AuthorSearchDict
If kvp.Key <> "" Then
loc = strTemp.IndexOf(kvp.Key)
If loc > -1 Then
strTempAuthor = IIf(cbFNF.Checked, AuthorDict(AuthorSearchDict(kvp.Key)).FNL, AuthorDict(AuthorSearchDict(kvp.Key)).LNF).ToString
If strAuthorsOut = Nothing Then
strAuthorsOut &= strTempAuthor & "; "
ElseIf strAuthorsOut.IndexOf(strTempAuthor, myOptions) = -1 Then 'fix 1
strAuthorsOut &= strTempAuthor & "; "
End If
End If
End If
Next
If Len(strAuthorsOut) > 0 Then strAuthorsOut = Microsoft.VisualBasic.Left(strAuthorsOut, Len(strAuthorsOut) - 2)
Return strAuthorsOut
End Function
Any ideas short of my continued brute force.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|