|
-
Aug 3rd, 2005, 02:09 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Fast Letters Only Validation
Hi,
i am wanting to detect whether a short string has numeral or operation characters within it, so that i may reject this string, 1000s of these will be sifted through, therefore i am wanting this to be as fast as possible.
Oviously checking the ascii codes of each character will not be efficient.
Using multiple Instr or Replace functions would not be efficient either, would it?
Please note, a string may contain letters and numbers, but is still rejected because of the numbers.
What do you recommend?
-
Aug 3rd, 2005, 02:32 PM
#2
Fanatic Member
Re: Fast Letters Only Validation
The only way I know of to do it is to check each letter in the string... if they are short strings then the efficiency of it shouldn't be a huge issue..
Now if you only want letters a to z... and anything else would be unacceptable then you can do this..
VB Code:
Function AllLetters(tString As String) As Boolean
Dim strLetters As String
Dim i As Integer
strLetters = "abcdefghijklmnopqrstuvwxyz"
For i = 1 To Len(tString)
If Instr(strLetters, LCase(Mid(tString, i, 1))) = 0 Then
AllLetters = False
Exit Function
End If
Next
AllLetters = True
End Function
If you know for sure it's only going to be numbers that make it unacceptable you can do it slightly different.
VB Code:
Function AllLetters(tString As String) As Boolean
Dim i As Integer
For i = 1 To Len(tString)
If IsNumeric(Mid(tString, i, 1)) Then
AllLetters = False
Exit Function
End If
Next
AllLetters = True
End Function
-
Aug 3rd, 2005, 03:23 PM
#3
Thread Starter
Frenzied Member
Re: Fast Letters Only Validation
steven,
thanks alot for the idea's and samples.
although still requiring comparison for each char, your first sample (where i wouldve used ascii) allows for an easy method to also allow - and ' without a seperate comparison to the alphabet, unlike the way i wouldve done it with ascii.
seems like the best way to do it so far doesn't it, and peformance wise, i tested it out, and no noticable lag with it.
I only made one slight change, which was to scrap lcase and mid
lcase because i forgot to specify this has already been performed, and mid to the .net method .substring
VB Code:
Function AllLetters(ByVal tString As String) As Boolean
Dim strLetters As String
Dim i As Integer
strLetters = "abcdefghijklmnopqrstuvwxyz-'"
Try
For i = 0 To Len(tString) - 1
If InStr(strLetters, tString.Substring(i, 1)) = 0 Then
AllLetters = False
Exit Function
End If
Next
Catch
MsgBox(Err.Description)
End Try
AllLetters = True
End Function
-
Aug 10th, 2005, 01:35 AM
#4
Re: [RESOLVED] Fast Letters Only Validation
I have a couple of points for you Phill. Normally you wouldn't notice a difference but if you are calling this function many times, it is actaully more efficient to use a Return statement than it is to use an Exit Function statement or allow execution to fall to the End Function statement.
Also, you might like to test these variations to see if there is any appreciable difference in execution time:
VB Code:
Function IsValidString(ByVal text As String) As Boolean
If text Is Nothing Then
Return True
End If
Dim validChars As Char() = "abcdefghijklmnopqrstuvwxyz-'".ToCharArray()
For i As Integer = 0 To text.Length - 1
If Array.IndexOf(validChars, text.Chars(i)) = -1 Then
Return False
End If
Next i
Return True
End Function
VB Code:
Function IsValidString(ByVal text As String) As Boolean
If text Is Nothing Then
Return True
End If
Dim validChars As Char() = "abcdefghijklmnopqrstuvwxyz-'".ToCharArray()
Dim textChars As Char() = text.ToCharArray()
For Each ch As Char In textChars
If Array.IndexOf(validChars, ch) = -1 Then
Return False
End If
Next ch
Return True
End Function
I'm quite sure that you could use regular expressions to accomplish the same task, but I don't know whether it would be faster or not. I don't know regular expressions well enough to give you the correct syntax but you may like to lok into it.
-
Aug 10th, 2005, 07:07 AM
#5
Thread Starter
Frenzied Member
Re: [RESOLVED] Fast Letters Only Validation
Thanks Jm,
i have used Return now, as for char arrays, i believe the only advantage is memory here, when i am heavy into this project again i will test actual times and see if this is the case or not.
-
Aug 10th, 2005, 08:25 AM
#6
Re: [RESOLVED] Fast Letters Only Validation
The advantage of char arrays is actually the fact that you don't have to repeatedly call methods or properties, which take longer than array access via an index. Whether that difference is significant is the question.
-
Aug 11th, 2005, 12:01 PM
#7
Re: [RESOLVED] Fast Letters Only Validation
Using jmcilhinney's idea of using regex:
VB Code:
Dim str As String = System.Text.RegularExpressions.Regex.Match(sInputstring, "[\w\-']+").ToString
If str.Length <> sInputstring.Length Then Return False
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
|