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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first character 1

Status
Not open for further replies.

Oostwijk

Technical User
Oct 19, 2003
82
0
0
NL
How to check if the first character in a string is a number ?
 
Code:
if (/^\d/)
{
....
}
Will work I /think/. I could be wrong.
 
where in the code can I place the string ?
 
cgimonk's code assumes that your string is $_. To use another:
Code:
if ($string =~ /^\d/)
{
....
}
 
It doesn't seem to work, anyone got another suggestion ?
 
Worked for me, maybe your string is really NOT a number?

Code:
$string = "1234";
if ($string =~ /^\d/)
{
print "I am a number";
} else { print "I am NOT a number";}
 
It should work, if the first char is really a digit.
Is there whitespace before the number? Then you might try
Code:
if ($string =~ /^\s*\d/)
{
....
}
This allows for zero or more whitepace chars before the digit, so will work if there's whitespace before or not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top