This time let's look at the string object.
A string is a sequence of characters.
In other words, by using the string object and the methods it owns,
you can program with strings.
For instance, when a user has written an email,
you could check in advance whether they wrote it correctly,
or — if you do more detailed and meticulous work — even something like spell-checking should be plenty possible.
(Whether useful or not...)
|
|
<!DOCTYPE html> |
|
|
<html> |
|
|
<head> |
|
|
<meta charset="utf-8" /> |
|
|
<title>string object</title> |
|
|
<script type="text/javascript"> |
|
|
//<![CDATA[ |
|
|
|
|
|
//string has the length property and the indexOf / lastIndexOf methods. |
|
|
|
|
|
var str1, str2; |
|
|
str1 = new String("javascript"); |
|
|
str2 = "DOMScript"; |
|
|
|
|
|
document.write("<p>" + str1.length + "</p>"); |
|
|
document.write("<p>" + str2.length + "</p>"); |
|
|
document.write("<p>" + str1.indexOf("s") + "</p>"); |
|
|
|
|
|
var email="testtest.co.kr"; |
|
|
var check=email.indexOf("@");// returns -1 if not present. !!! |
|
|
|
|
|
if(check == -1) |
|
|
{ |
|
|
alert("The email format is not valid."); |
|
|
} |
|
|
|
|
|
//]]]]> |
|
|
</script> |
|
|
</head> |
|
|
|
|
|
<body> |
|
|
|
|
|
</body> |
|
|
</html> |
