Solved

IsAlphabet(string) ??

Posted on 2000-03-02
19
1,393 Views
Last Modified: 2008-03-04
In VB6 Enterprice, we can used IsNumeric(string) to check string with all numeric.
Is there any built-in function to check a string with all alphabet?
or please suggest a best solution.

TQ;
0
Comment
Question by:pkimk
  • 7
  • 4
  • 4
  • +3
19 Comments
 
LVL 1

Expert Comment

by:wolfjjj
Comment Utility
heres a quick piece of ungly code that will only allow characters a to z and A to Z

Public Function IsAlpha(inString As String) As Boolean
  strlen = Len(inString)
  alpha = True
 
  For i = 1 To strlen
    thechar = Mid(inString, i, i)
    If (Asc(thechar) <= Asc("a") Or Asc(thechar) >= Asc("z")) And _
       (Asc(thechar) <= Asc("A") Or Asc(thechar) >= Asc("Z")) Then
      alpha = False
    End If
  Next i
 
  IsAlpha = alpha
End Function


havent tested it very much *shrug* might be what u want
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 20 total points
Comment Utility
Oh wow.. 20 points!!!.. LOL. Nevertheless.. No.. there is no alpha test.. <sigh>.. But the function below should allow you to test for any characters in a string that you like.. <smile>.

<---------- Code ---------->

Public Function xIsAlpha _
(Byval strInput as String) _
as Boolean

Const strTEST = "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]" ' Or any other characters you like

xIsAlpha = True
Dim intIndex As Integer
For intIndex = 1 To Len(strInput)
'   Ucase used to test for both upper and lower case
    If Not UCase(Mid(strInput, intIndex, 1)) Like strTEST _
    Then
        xIsAlpha = False
        Exit For
    End If
Next intIndex

End Function

0
 
LVL 3

Expert Comment

by:paulstamp
Comment Utility
Wolfjjj - according to the rules of EE you shouldnt lock a question unless you're absolutely sure it is the right answer....

However I reckon you're pretty much right - but could be simplified slightly using lcase$ so you only need to test for lower case letters :

Public Function IsAlpha(byval inString As String) As Boolean
  strlen = Len(inString)
  alpha = True
   
  inString = lcase$(instring)

  For i = 1 To strlen
    thechar = Mid(inString, i, i)
    If (Asc(thechar) <= Asc("a") Or Asc(thechar) >= Asc("z")) Then
      alpha = False
    End If
  Next i
   
  IsAlpha = alpha
End Function


0
 
LVL 3

Expert Comment

by:paulstamp
Comment Utility
Just noticed... Wolfjjj - your code wont actually work - the 2nd param in mid$ should be a "1", not "i"

Public Function IsAlpha(byval inString As String) As Boolean
   
  inString = lcase$(instring)

  For i = 1 To Len(inString)
    thechar = Mid$(inString, i, 1)
    If thechar <= "a" Or thechar >= "z" Then
        isalpha = False
        Exit For
    Endif
 
  Next i
   
End Function




0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Psssst.. Paulstamp.. don't forget to add a IsAlpha = True before your loop.. <wink>. Also.. move your Lcase down to your actual test (which should NOT have the = sign in it). Here.. try this out.. <smile>

<----- Code ----->

Public Function IsAlpha(byval inString As String) As Boolean
     
  isAlpha = True
  For i = 1 To Len(inString)
    If Lcase(Mid$(inString, i, 1))      < "a" _
    Or Lcase(Mid$(inString, i, 1)) > "z" _
    Then
       isalpha = False
       Exit For
    End If
  Next i
     
End Function
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
<laughing>.. Yikes look at me.. I forgot to declare the index... LOL. Please add another line to the beginning of the function.

Dim i as Integer

<sheesh>
0
 
LVL 3

Expert Comment

by:paulstamp
Comment Utility
Oops.. quite right.. missed out the Setting = True at the top, the "<=" I copied and pasted from Wolfjjj's code (my original comment was a modification of his code to make it work more efficiently)

Why move the lcase$ to the actual test - that means performing the conversion every time around the loop instead of just once ?
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Here's my entry.  It has an optional second parameter to allow the space character as valid.

Public Function is_alphabet(ByRef sTest As String, Optional vAllowSpaces) As Boolean

    Dim c As Long
    Dim sChar As String * 1
    Dim bSpaces As Boolean
   
    If IsMissing(vAllowSpaces) Then
        bSpaces = False
    Else
        bSpaces = CBool(vAllowSpaces)
    End If
   
    For c = 1 To Len(sTest)
        sChar = UCase(Mid(sTest, c, 1))
        If (sChar < "A" Or sChar > "Z") And _
        (sChar <> Chr(32) Or bSpaces = False) Then
           
            is_alphabet = False
            Exit Function
           
        End If
    Next
    is_alphabet = True
       
       

End Function


MsgBox is_alphabet("Fred Bloggs", True)
MsgBox is_alphabet("Fred Bloggs", False)
MsgBox is_alphabet("FredBloggs")
MsgBox is_alphabet("Fred Bloggs")
MsgBox is_alphabet("Fred Bloggs55")
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
I did it for readibility.. more than anything else.. and to keep the code simple and short. The Like method I proposed in my submittal is consistant with this. In one line (const strTEST), it shows all the characters being tested for.. any character the developer desires.. without having to do a bunch of asc/char conversions.

With long strings, you are correct in assuming that precasing the string before the tests would provide a performance benefit. But frankly, with 650mhz screamers now being offered by Gateway for less than $1000, I doubt that in the future, anyone will notice the difference in readable/maintainable code versus code that is terribly tight.

All in all, its a tradeoff.. performance vs readability/maintainability.. and really just up to the developers choice.

Thank YOU for asking.. <smile>
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Hello Deighton.. <smiling>. To add spaces (or anything else in my Like example).. just insert the character in the strTEST constant anywhere between the brackets. Eg...

const strTEST = "[ABCDEFG /1234*^HIJKLMNOPQRSTUVWXYZ]"

and only those characters will be tested as being valid.
0
 
LVL 3

Expert Comment

by:paulstamp
Comment Utility
wsh2... agreed - I think your answer is a neater solution than mine in as much is it is extendible to allow extra chars without messing around with additional if statements.

I take your point about the performance of newer machines but I cant help thinking that is Microsoft's approach to performance <smile>
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Its a chicken and egg argument.. Microsoft would never have gotten where it did without Intel.. and likewise. Intel would have been nothing without Microsoft. We both should be happy that today, we don't have to squeeze our code into a 16kb 8088 machine that has two flat tires.. <smile>.

Think for a moment.. the hot machine of 10 years ago.. was a 16mhz 80286.. now, how much software do you run today from that era? You can bet your bottom dollar.. that it was written tight.. but today.. who cares?.. <wink>.
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
You could always do it like this:

Function IsAlpha(Source As String) As Boolean
    Dim iVal As Long
    IsAlpha = True
    For iVal = 1 To Len(Source)
        Select Case Asc(UCase(Mid$(Source, iVal, 1)))
            Case 32, 65 To 90: Case Else
                IsAlpha = False
                Exit For
        End Select
    Next iVal
End Function


This will allow A-Z, a-z and space....
If you want any other characters to be allowed, put them in the 1st case statement...


Cheers!
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Very nice mcrider.. <smile>
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
wsh2, Thanks!  I tend to think in cases instead of ifs... ;-)
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
Too bad the question is already locked... but pkimk is a seasoned user...


Cheers!
0
 

Author Comment

by:pkimk
Comment Utility
Huh guys, thanks all.

I like wsh2's solution. It's simple, short, readability and maintainability.

I learn more from mcrider and paulstamp's solution.
mcrider's solution is good!
I'll ask for help to credit 10 points each to two of you.

So sad to comment deighton's solution like drawing "snake with leg".
Anyhow, deighton, thank you very much for your posting.
Thank you wolfjjj.

cheers;
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
If you want to credit 10 points to both mcrider and paulstamp, just open 2 new questions in this category with the titles "FOR MCRIDER ONLY" and "FOR PAULSTAMP ONLY", and assign the points...


Cheers!®©
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
Thanks a bundle, I won't bother with you in future :O(
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now