Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check first character in a string

Status
Not open for further replies.

lyric0n

Technical User
Dec 28, 2005
74
I am having a problem with a couple of things...

What is the most efficent way to check if the first character in a string is equal to either an interger (0 through 9) or if it is equal to a letter (A through Z).

I appreciate the help...

Thanks.
 
So if I understand you correctly, if the first character is an integer you want it to do option A, and if its a letter you want it to do option B. Is that correct?
 
Finding out if it is a number is easy with IsNumeric. however to determine if it is an letter and not punctuation you will need to look at the ascii value and compare that to the range of ascii characters for letters.


Code:
'==========================================================================
'
' NAME: CheckNumberOrAlpha.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYRIGHT (c) 2006 All Rights Reserved
' DATE  : 6/13/2006
'
' COMMENT: Checks if a character is a number or alpha character
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================

string1 = "99LuftBaloons"
string2 = "Text string"

WScript.Echo CheckLeft(string1)
WScript.Echo CheckLeft(string2)


Function CheckLeft(stringtocheck)

If IsNumeric(Left(stringtocheck,1)) Then
	CheckLeft = "Number"
ElseIf Asc(left(stringtocheck,1)) >64 And Asc(left(stringtocheck,1))<91 _ 
	Or Asc(left(stringtocheck,1)) >96 And Asc(left(stringtocheck,1))<123 Then
   CheckLeft = "Alpha"
End If
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
COuldnt you also setup an array with A-Z and another array 0-9 and then grab the first character and try to match it against both arrays? If it matches the "letter" array, do this and if it matches the numeric array, do that.

Wouldnt that work?
 
An alternative which may also be illustrative of proper use of instr.
[tt]
's given being a string (may be null, empty, zero length)
ch=left(s,1)
ipos=instr(1,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",ch,0)
btest=not(isnull(len(s))) and (ipos<>0)
if btest then
wscript.echo "test : positive"
else
wscript.echo "test : negative"
end if
[/tt]
(Regexp is another approach for sure, I just don't feel interesting for this case.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top