String handling Examples
Before we go our separate ways, wouldn't you like to see a couple of examples on the utilization of these methods? Of course you would, and here they are!
-Loose Email Check Example
Let's start with a classic. This first example loosely checks the content of a form field to ensure it's a valid email address, by using indexOf() to check for the presence of the "@" and "." character:
Note that I said "loose." "Simple" also is the script. All that's involved is indexOf() and the number "-1" (which indicates the searched characters are not present inside the form field).
-Word Count Example
Counting the number of words in any given text can be tedious a task...without the help of the split() method, that is. Here's an example using it (note the simplicity):
Gotta love split() (and this tutorial!).