|
-
Jun 4th, 2007, 02:43 AM
#1
Thread Starter
New Member
check whether a string is alphabetic..help please
hi all..
im trying to check the input from user , so that the input all are alphabetic. how could i do this? i found a source code as below but i stil do not know how to do..
Code:
Function IsCharType(Char As String, ByVal CharType As Integer) As Boolean
Static cTypes(0 To 255) As Integer
If cTypes(0) = 0 Then
Dim i As Integer, v As Variant
For i = 65 To 90: cTypes(i) = 1: Next
For i = 97 To 122: cTypes(i) = 2: Next
For i = 40 To 57: cTypes(i) = 4 + 8 + 16: Next
cTypes(Asc("+")) = 8
cTypes(Asc("-")) = 8
cTypes(Asc(Format$(0.1, "."))) = 8
For Each v In Array(65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102)
cTypes(v) = cTypes(v) Or 16
Next
For Each v In Array(0, 9, 10, 13, 32)
cTypes(v) = cTypes(v) Or 32
Next
For Each v In Array(46, 44, 59, 58, 63, 33, 45, 39, 40, 41, 123, 125)
cTypes(v) = cTypes(v) Or 64
Next
End If
IsCharType = (cTypes(Asc(Char)) And CharType)
End Function
-
Jun 4th, 2007, 04:01 AM
#2
Re: check whether a string is alphabetic..help please
ok checking input AFTER it is already input is the wrong way to do it.
If you are getting input from a Textbox, then here is some example code for the keydown event that prevents anything besides letters from being entered
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 65 Or KeyAscii > 122 Then KeyAscii = 0
End Sub
-
Jun 4th, 2007, 04:07 AM
#3
Re: check whether a string is alphabetic..help please
This one will help u.But you have to check while pasting also
vb Code:
Private Declare Function IsCharAlpha Lib "user32" Alias "IsCharAlphaA" (ByVal cChar As Byte) As Long
Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsCharAlpha(KeyAscii) Then
'do nothing
ElseIf KeyAscii = 8 Then
'allow backspace
Else
KeyAscii = 0
End If
End Sub
Dana
Please mark you thread resolved using the Thread Tools as shown
-
Jun 4th, 2007, 04:10 AM
#4
Re: check whether a string is alphabetic..help please
Solution #2: Recieve input from the Microsoft Masked Edit Control 6.0 (add the reference to your tool bar)
It can be set to take only numbers (#), Letters and numbers (a or A), or just letters (& or C). Read the help file hidden in your c:\windows\help directory called msmask98.chm and read on the mask property. It has a bunch more options.
Finally, you don't have to worry about paste. It checks those too.
-
Jun 4th, 2007, 04:17 AM
#5
Re: check whether a string is alphabetic..help please
To change my code above to accept backspace:
If KeyAscii <> 8 Then If KeyAscii < 65 Or KeyAscii > 122 Then KeyAscii = 0
-
Jun 4th, 2007, 04:23 AM
#6
Fanatic Member
Re: check whether a string is alphabetic..help please
This might do the trick.
Just alter the Const string to add or remove characters that you will allow
Code:
Option Explicit
Const VALID_CHARS As String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 26 Then ' if it's not a control code
If InStr(VALID_CHARS, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End If
End Sub
-
Jun 4th, 2007, 04:54 AM
#7
Re: check whether a string is alphabetic..help please
heh heh heh I would be interested in finding out which solution was used
-
Jun 4th, 2007, 11:13 AM
#8
Re: check whether a string is alphabetic..help please
 Originally Posted by Lord Orwell
To change my code above to accept backspace:
If KeyAscii <> 8 Then If KeyAscii < 65 Or KeyAscii > 122 Then KeyAscii = 0
Select Case is generally faster than nested IFs and it's much easier to read (especially if you nest very deep:
Code:
Select Case KeyAscii
'in order of must used first makes it a little faster
Case 97 To 122 'a-z
Case 65 To 90 'A-Z
Case 8 'Backspace
Case Else
KeyAscii = 0
End Select
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 4th, 2007, 12:19 PM
#9
Re: check whether a string is alphabetic..help please
if you think select case is faster than nested ifs you should try it yourself in a loop. Nested ifs are faster. Select case always checks every comparison so it is equivalent of however many separate if-then statements.
But don't take my word for it. Here is both methods in a loop for you with a timed output. Guess which is faster.
Code:
KeyAscii = 1
stored = Timer
For cl = 1 To 100000
Select Case KeyAscii
'in order of must used first makes it a little faster
Case 97 To 122 'a-z
Case 65 To 90 'A-Z
Case 8 'Backspace
Case Else
KeyAscii = 0
End Select
Next cl
MsgBox Timer - stored
stored = Timer
For cl = 1 To 100000
If KeyAscii <> 8 Then If KeyAscii < 65 Or KeyAscii > 122 Then KeyAscii = 0
Next cl
MsgBox Timer - stored
-
Jun 4th, 2007, 03:21 PM
#10
Re: check whether a string is alphabetic..help please
I see a significant difference (I lengthened the run to get a better difference - each loop takes around 15 seconds on this computer), but in favor of Select
Code:
Private Sub Command3_Click()
Dim KeyAscii As Integer, stored As Single, cl As Long
stored = Timer
For cl = 1 To 100000000
KeyAscii = 98
Select Case KeyAscii
'in order of most used first makes it a little faster
Case 97 To 122 'a-z
Case 65 To 90 'A-Z
Case 8 'Backspace
Case Else
KeyAscii = 0
End Select
KeyAscii = 0
Next cl
MsgBox Timer - stored
stored = Timer
For cl = 1 To 100000000
KeyAscii = 98
If KeyAscii <> 8 Then If KeyAscii < 65 Or KeyAscii > 122 Then KeyAscii = 0
KeyAscii = 0
Next cl
MsgBox Timer - stored
End Sub
For KeyAscii = 1, the top loop time is about doubled. (See my comment in the Select structure.)
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 4th, 2007, 04:29 PM
#11
Re: check whether a string is alphabetic..help please
 Originally Posted by Lord Orwell
Select case always checks every comparison
Select Case only checks each Case until a match is found. Any Case beyond that is not executed. In your test procedure, every case was evaluated because none match. I'll also point out that Select Case is not an alternative for nested If statements. It functions similar to a block If statement with multiple ElseIf clauses.
-
Jun 4th, 2007, 07:05 PM
#12
Re: check whether a string is alphabetic..help please
 Originally Posted by janiceteh04
hi all..
im trying to check the input from user , so that the input all are alphabetic. how could i do this? i found a source code as below but i stil do not know how to do..
Code:
Function IsCharType(Char As String, ByVal CharType As Integer) As Boolean
Static cTypes(0 To 255) As Integer
If cTypes(0) = 0 Then
Dim i As Integer, v As Variant
For i = 65 To 90: cTypes(i) = 1: Next
For i = 97 To 122: cTypes(i) = 2: Next
For i = 40 To 57: cTypes(i) = 4 + 8 + 16: Next
cTypes(Asc("+")) = 8
cTypes(Asc("-")) = 8
cTypes(Asc(Format$(0.1, "."))) = 8
For Each v In Array(65, 66, 67, 68, 69, 70, 97, 98, 99, 100, 101, 102)
cTypes(v) = cTypes(v) Or 16
Next
For Each v In Array(0, 9, 10, 13, 32)
cTypes(v) = cTypes(v) Or 32
Next
For Each v In Array(46, 44, 59, 58, 63, 33, 45, 39, 40, 41, 123, 125)
cTypes(v) = cTypes(v) Or 64
Next
End If
IsCharType = (cTypes(Asc(Char)) And CharType)
End Function
It builds a data structure to check against for only one character. Every time you call the function for another character the data structure for check is rebuilt. Inefficient for long strings, you iterate the string and pass Mid(str, pos, 1) to this function. EDIT: my bad, accidentally skipped over Static declaration.
CharType implementation would have been better with an enumeration... the treatment of the values or the bit representation/interpretation would have then been self explanatory.
Bear in mind that characters typically are composed of two bytes, code above could have:
- set up cTypes() with same scope as string, or pass reference to string so they have same scope and do away with repeated calls.
- passed the string to a byte array, eg. bytArr() = yourstring, then iterate step 2
- go straight to byte AND and do away with Asc() function.
Last edited by leinad31; Jun 4th, 2007 at 09:54 PM.
-
Jun 4th, 2007, 07:48 PM
#13
Re: check whether a string is alphabetic..help please
ok "Logophobic" wow you picked your name correctly 
al42
recheck your results. With your code and ascii 98 i tested this:
loop 1: 15.07813
loop 2: 14.79688
when i changed the ascii code to a capital letter the difference was more pronounced:
loop 1: 19.00000
loop 2: 14.73438
And when i changed it to a backspace oh mercy
loop 1: 22.87500
loop 2. 9.921875
I have yet to manage to get the 2nd sub slower than the first and my entire line is executing while yours is running the first choice only.
And i could in fact optimize mine more with an if else in place of the or.
I don't know why you don't like if statements, but anyone who programs games will tell you to avoid select case because it is the slowest method in vb of making a choice. And i program games. Don't take my word on it. I am sure you will find plenty of threads on optimizing code for speed.
-
Jun 4th, 2007, 08:48 PM
#14
Hyperactive Member
Re: check whether a string is alphabetic..help please
 Originally Posted by Al42
In favor of Select
Sorry to butt in here, but I thought to make a more fair test by using random characters. Each test uses the same set in the same order.
I found
Select CASE took 64.5 seconds
IF statement took 59.3 seconds
Clever method by me 69.0 seconds
Conclusion:
1) Select case takes negligibly longer than IF
2) I am not clever
Mac
Code:
Private Sub Command3_Click()
Const LoopMax = 100000000
Dim c(63) As Integer 'Cases to test
Dim i As Integer
For i = 1 To 26: c(i) = 96 + i: Next i
For i = 1 To 26: c(i + 26) = 64 + i: Next i
For i = 1 To 10: c(i + 52) = 47 + i: Next i
c(63) = 8
Dim Keyascii As Single, stored As Single, cl As Long
Dim x As Integer, y As Integer
Dim Dummy As Boolean
stored = Timer: x = 0: y = 0: Rnd (-1234587)
For cl = 1 To LoopMax
x = x + 1: If x > 10000 Then x = 0: y = y + 1: Label1.Caption = Str$(y): DoEvents
Keyascii = c(1 + Int(Rnd * 63))
Dummy = True
Select Case Keyascii
Case 97 To 122: 'a-z
Case 65 To 90: 'A-Z
Case 8: 'Backspace
Case Else: Dummy = False
End Select
Next cl
Label1.Caption = Timer - stored
stored = Timer: x = 0: y = 0: Rnd (-1234587)
For cl = 1 To LoopMax
x = x + 1: If x > 10000 Then x = 0: y = y + 1: Label2.Caption = Str$(y): DoEvents
Keyascii = c(1 + Int(Rnd * 63))
Dummy = False
If Keyascii <> 8 Then If Keyascii < 65 Or Keyascii > 122 Then Dummy = True
Next cl
Label2.Caption = Timer - stored
stored = Timer: x = 0: y = 0: Rnd (-1234587)
Dim Z(255) As Boolean
For i = 1 To 26: Z(96 + i) = True: Next i
For i = 1 To 26: Z(64 + i) = True: Next i
For i = 1 To 10: Z(47 + i) = True: Next i
Z(8) = True
For cl = 1 To LoopMax
x = x + 1: If x > 10000 Then x = 0: y = y + 1: Label3.Caption = Str$(y): DoEvents
Keyascii = c(1 + Int(Rnd * 63))
Dummy = Z(Keyascii)
Next cl
Label3.Caption = Timer - stored
End Sub
-
Jun 4th, 2007, 08:57 PM
#15
Re: check whether a string is alphabetic..help please
based on those times, i estimate you are running...
a 2ghz pentium 4 But why did you limit the random to 1 to 63? That excludes all letters. It should be 1 to 124
Last edited by Lord Orwell; Jun 4th, 2007 at 09:00 PM.
-
Jun 4th, 2007, 09:02 PM
#16
Hyperactive Member
Re: check whether a string is alphabetic..help please
 Originally Posted by Lord Orwell
based on those times, i estimate you are running...
a 2ghz pentium 4 
Talking to me?
"I use vb6 , Office 2k, DX7 and WinXP (i am old fashioned)"
Oh, yeah!
I use vb5, WindowsNT - I can out-old-fashioned anyone - I code mostly in DOS QBasic.
But the reason I came back was to note that my test was a little off because the IF statement failed to eliminate the cases between a-z and A-Z.
Conclusion:
Select Case may be a bit slower, but it is far less bug-prone.
Mac
-
Jun 4th, 2007, 09:02 PM
#17
Re: check whether a string is alphabetic..help please
sorry. It should be 1 to 122
-
Jun 4th, 2007, 10:30 PM
#18
Re: check whether a string is alphabetic..help please
I'd hate to have to debug code with a bunch of nested if statements.
-
Jun 4th, 2007, 11:26 PM
#19
Re: check whether a string is alphabetic..help please
 Originally Posted by DigiRev
I'd hate to have to debug code with a bunch of nested if statements.
They're arguing over timing results and ignored scenario for its implemetation. Seriously, can anyone type so fast that the KeyPress() event gets fired continously and the timing difference between If and Select Case is felt. Hehehehe.
Code readability and ease of maintenance has more benefit in this case IMO.
-
Jun 4th, 2007, 11:30 PM
#20
Re: check whether a string is alphabetic..help please
if you stretch them out downward they look exactly like a select case
if a = 1 then
code
elseif a = 2 then
code
elseif a = 3 then
code
end if
You think this is bad, try debugging code stretched through 20 or 30 custom functions, instead all in one.
-
Jun 4th, 2007, 11:37 PM
#21
Re: check whether a string is alphabetic..help please
 Originally Posted by Mr.Mac
Talking to me?
"I use vb6 , Office 2k, DX7 and WinXP (i am old fashioned)"
Oh, yeah!
I use vb5, WindowsNT - I can out-old-fashioned anyone - I code mostly in DOS QBasic.
Let's not go there. I can program in assembly for both DOS and commodore 64 computers. And i am intimately familiar with QBasic. FYI if you can find a copy of vb1.0 for dos, you can turn qbasic programs into .exe 
I recommend you upgrade to win2k. Less bugs, and will run anything (with sp4 installed). and uh er, I have dos 6.22 and win 98 both installed under virtual pc (and believe me networking virtual pc is a pain in the can) I have actually had much more luck getting dos programs to work inside a nifty little program called DosBox. It is free and opensource and i have yet to find anything that won't run in it. Ultima 8, Pool of Radiance, etc. It's all good! 
Mac
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
|