Categories:

String handling methods of the String object

Here are the string handling related methods of the Stirng object, in full force:

Methods Description
charAt(x) Returns the character at the "x" position within the string.
charCodeAt(x) Returns the Unicode value of the character at position "x" within the string.
concat(v1, v2,...) Combines one or more strings (arguments v1, v2 etc) into the existing one and returns the combined string. Original string is not modified.
fromCharCode(c1, c2,...) Returns a string created by using the specified sequence of Unicode values (arguments c1, c2 etc). Method of String object, not String instance. For example: String.fromCharCode().
indexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. "Start" is an optional argument specifying the position within string to begin the search. Default is 0.
lastIndexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. Searches the string from end to beginning. If not found, -1 is returned. "Start" is an optional argument specifying the position within string to begin the search. Default is string.length-1.
match(regexp) Executes a search for a match within a string based on a regular expression. It returns an array of information or null if no match is found.
replace( regexp, replacetext) Searches and replaces the regular expression portion (match) with the replaced text instead.
search(regexp) Tests for a match in a string. It returns the index of the match, or -1 if not found.
slice(start, [end]) Returns a substring of the string based on the "start" and "end" index arguments, NOT including the "end" index itself. "End" is optional, and if none is specified, the slice includes all characters from "start" to end of string.
split(delimiter, [limit]) Splits a string into many according to the specified delimiter, and returns an array containing each element. The optional "limit" is an integer that lets you specify the maximum number of elements to return.
substr(start, [length]) Returns the characters in a string beginning at "start" and through the specified number of characters, "length". "Length" is optional, and if omitted, up to the end of the string is assumed.
substring(from, [to]) Returns the characters in a string between "from" and "to" indexes, NOT including "to" inself. "To" is optional, and if omitted, up to the end of the string is assumed.
toLowerCase() Returns the string with all of its characters converted to lowercase.
toUpperCase() Returns the string with all of its characters converted to uppercase.

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(position)

charAt() simply returns the character at the specified position. For example:

var message="internet"
//alerts "n"
alert(message.charAt(1))

-concat(v1, v2,..)

A "redundant" method, concat() combines the strings of its 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.

-indexOf(char/substring)

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.

-slice(start, end)

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?

-split(delimiter)

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:

var message="Welcome to JavaScript Kit"

Let's say I want to extract out the individual words ("Welcome", "to", etc) from it. I would do the following:

//word[0] contains "Welcome", word[1] 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:

var message="Welcome to JavaScript Kit"
//word[0] contains "We", word[1] contains "come to JavaScript Kit"
var word=message.split("l")

The split() method is often used to parse values stored inside a cookie, since they are by default separated by semicolons (;), a set delimiter.

-substring(from, to)

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!