Java Reference
In-Depth Information
Try It Out Checking a Character's Case
The following is an example that detects the type of the character at the start of a given string — that is,
whether the character is uppercase, lowercase, numeric, or other.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 5: Example 2</title>
<script type=”text/javascript”>
function checkCharType(charToCheck)
{
var returnValue = “O”;
var charCode = charToCheck.charCodeAt(0);
if (charCode >= “A”.charCodeAt(0) && charCode <= “Z”.charCodeAt(0))
{
returnValue = “U”;
}
else if (charCode >= “a”.charCodeAt(0) && charCode <= “z”.charCodeAt(0))
{
returnValue = “L”;
}
else if (charCode >= “0”.charCodeAt(0) && charCode <= “9”.charCodeAt(0))
{
returnValue = “N”;
}
return returnValue;
}
</script>
</head>
<body>
<script type=”text/javascript”>
var myString = prompt(“Enter some text”,”Hello World!”);
switch (checkCharType(myString))
{
case “U”:
document.write(“First character was upper case”);
break;
case “L”:
document.write(“First character was lower case”);
break;
case “N”:
document.write(“First character was a number”);
break;
default:
document.write(“First character was not a character or a number”);
}
</script>
Search WWH ::




Custom Search