jiniasp
asked on
first character in a string
hi,
how do i find in javascript whether the first character in a string is a number,alphabet,or a special character?
how do i find in javascript whether the first character in a string is a number,alphabet,or a special character?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use isNaN(mystring.substring(0 ,1))
As to the above code. It uses regular expression:
/^[a-z]/i can be explained as:
^ - start of string
[a-z] - a single a-z character
/i - case insensitive search
\d is shorthand for [0-9], resulting in searching for a number at the start of a string: ^\d
-r-
As to the above code. It uses regular expression:
/^[a-z]/i can be explained as:
^ - start of string
[a-z] - a single a-z character
/i - case insensitive search
\d is shorthand for [0-9], resulting in searching for a number at the start of a string: ^\d
-r-
Put these special prototype functions anywhere in your javascript:
String.prototype.IsAlpha= function()
{
return (this.match(/[A-Za-z]/g) ? true : false);
}
String.prototype.IsNumeric = function()
{
return (this.match(/[0-9]/g) ? true : false);
}
String.prototype.IsSpecial = function()
{
return (this.match(/[^A-Za-z0-9]/ g) ? true : false);
}
These mean that when you have a string or a number variable, you can simply call stringvar.IsAlpha() stringvar.IsSpecial() or stringvar.IsNumeric() which return true or false accordingly.
with your string, since you want the first character only do the following:
var mystring = '0123AbCdEfG£$%^&';
var isNumeric = (mystring+'').charAt(0).Is Numeric(); // check for number
var isAlpha = (mystring+'').charAt(0).Is Alpha(); // check for alpha
var isSpecial = (mystring+'').charAt(0).Is Special(); // check for alpha
var isAlphaNumeric = (mystring+'').charAt(0).Is Alpha() || (mystring+'').charAt(0).Is Numeric(); // check for alpha
the only ones that will be true will be isNumeric and isAlphaNumeric, the others should be false.
you can also derive isSpecial by doing
var isSpecial = !(mystring+'').charAt(0).I sAlpha() && !(mystring+'').charAt(0).I sNumeric() ;
Similarly, like Roonaan's layout, you can use the statements in an if or switch statement:
if ((mystring+'').charAt(0).I sNumeric() )
{
// numeric
}
else if ((mystring+'').charAt(0).I sAlpha())
{
// alpha
}
Because these are prototypes of the base String object, this means you can use these as methods for *any* string, whether they are a variable, in an array or wherever.
In case you're wondering, I'm forcing (mystring+'') in the example above to force the variable into a string and test it, without changing the basic variable. If you try doing it with a number and you call the method, you'll get an exception.
String.prototype.IsAlpha= function()
{
return (this.match(/[A-Za-z]/g) ? true : false);
}
String.prototype.IsNumeric
{
return (this.match(/[0-9]/g) ? true : false);
}
String.prototype.IsSpecial
{
return (this.match(/[^A-Za-z0-9]/
}
These mean that when you have a string or a number variable, you can simply call stringvar.IsAlpha() stringvar.IsSpecial() or stringvar.IsNumeric() which return true or false accordingly.
with your string, since you want the first character only do the following:
var mystring = '0123AbCdEfG£$%^&';
var isNumeric = (mystring+'').charAt(0).Is
var isAlpha = (mystring+'').charAt(0).Is
var isSpecial = (mystring+'').charAt(0).Is
var isAlphaNumeric = (mystring+'').charAt(0).Is
the only ones that will be true will be isNumeric and isAlphaNumeric, the others should be false.
you can also derive isSpecial by doing
var isSpecial = !(mystring+'').charAt(0).I
Similarly, like Roonaan's layout, you can use the statements in an if or switch statement:
if ((mystring+'').charAt(0).I
{
// numeric
}
else if ((mystring+'').charAt(0).I
{
// alpha
}
Because these are prototypes of the base String object, this means you can use these as methods for *any* string, whether they are a variable, in an array or wherever.
In case you're wondering, I'm forcing (mystring+'') in the example above to force the variable into a string and test it, without changing the basic variable. If you try doing it with a number and you call the method, you'll get an exception.
ASKER
If u don't mind, could u please explain the code above.
Can isNaN(x) function be used to find whether first character is number or not?