String handling methods of the String object
Here are the string handling related methods of the Stirng object, in full force:
At first glance, the above methods can appear a little intimidating to some, so let's go over some of the more commonly used ones, method by method:
charAt() simply returns the character at the specified position. For example:
var message="internet" //alerts "n" alert(message.charAt(1))
A "redundant" method, concat() combines the strings of it's parameters with the calling string. Here's an example:
var message="Bob" var final=message.concat(" is a"," hopeless romantic.") //alerts "Bob is a hopeless romantic." alert(final)
The reason for this method's redundancy is that it's often a lot simpler to use the "+" operator instead to combine strings. The choice is yours, of course.
An extremely useful string method, indexOf() allows you to search whether a particular character or substring exists within a string, and if so, where the first character of the character/substring is located. If no match is found, "-1" is returned instead.
The below demonstrates using this method to determine whether the word "George" exists in a sentence:
var sentence="Hi, my name is George!" if (sentence.indexOf("George")!=-1) alert("George is in there!")
Since it does, indexOf() will NOT return -1, but rather, the index number of "G", and subsequently, the alert message will be executed.
If we were to tweak the sentence ever so slightly, so it reads:
var sentence="Hi, my name is Goerge!"
Running the same indexOf() code will return "-1" instead.
As the name implies, slice() extracts out a substring from the string as determined by the starting and ending points of it's parameters:
var text="excellent" text.slice(0,4) //returns "exce" text.slice(2,4) //returns "ce"
Simple enough, right?
One of my personal favorite, split() cuts up a string into pieces, using the delimiter as the point to cut off, and stores the results into an array. "Say that again?", you say. Consider the following message:
Let's say I want to extract out the individual words ("Welcome", "to", etc) from it. I would do the following:
//word contains "Welcome", word contains "to" etc var word=message.split(" ")
Variable word instantly becomes an array that holds the individual words. This is so because we used a space (" ") as the delimiter, which also is what's separating each word.
To hammer down the point, here's the same message again, manipulated by split() using a different delimiter this time:
The spit() method is often used to parse values stored inside a cookie, since they are by default separated by semicolons (;), a set delimiter.
Last but not least, we arrive at substring(). This method simply returns the substring beginning with the "from" parameter (included as part of the substring), and ending with "to" (NOT included as part of substring). It behaves just like the slice() method seen earlier. For example:
var text="excellent" text.substring(0,4) //returns "exce" text.substring(2,4) //returns "ce"
Exhale. We're at the end of the tunnel!