Think JavaScript's only good for image swaps and tickertapes?Think again - the language comes with a powerful String() objectdesigned to help you quickly and efficiently perform string manipulationtasks in the client. This article explains how, with illustrations andcode samples.
Let's start with the basics. In JavaScript, the term "string" refers to a sequence of characters. The following are all valid examples of strings:
"hey wassup?" "I ROCK!" "a long time ago in a galaxy far, far away"
String values can be assigned to a variable using the standard assignment operator.
<script language="Javascript">
var myStr = "Elementary, my dear Watson";
</script>
String values may be enclosed in either double quotes
("") or single quotes('') - the following variable assignments are equivalent:
<script language="Javascript">
var myStr = "Elementary, my dear Watson";
var myStr = 'Elementary, my dear Watson';
</script>
This comes in handy when you need to embed HTML code
in your JavaScript strings, and don't want the various sets of quotes to conflict with each other. Consider the following example, which illustrates:
<script language="Javascript">
// bad
var myStr = "<a href="http://www.melonfire.com">Home</a>";
// good
var myStr = '<a href="http://www.melonfire.com">Home</a>';
</script>
You can also take the scenic route, and define a
string using JavaScript's built-in String object, as below:
<script language="Javascript">
var myStr = "Elementary, my dear Watson";
// this is equivalent
var myStr = new String();
myStr = "Elementary, my dear Watson";
// and so is this
var myStr = new String("Elementary, my dear Watson");
</script>
When you do this, you create a proper JavaScript
object, and thereby have access to all the methods and properties of that object. Note that it's not essential to create strings using the second technique outlined above; it's far more common (not to mention easier on your keyboard) to use the first one, because the JavaScript interpreter automatically converts string datatypes into string objects whenever necessary.
If you're a JavaScript newbie, this next example won't do much for you - but for those of you who are purists, this next code snippet will help in making the distinction between a datatype and an object clearer.
<script language="Javascript">
var myStr = "Hit me";
// this returns "string"
alert(typeof(myStr));
var myStr = new String("Hit me");
// this returns "object"
alert(typeof(myStr));
</script>
You can concatenate strings with the addition (+)
operator - as the following example demonstrates:
<script language="Javascript">
var fname = "James";
var lname = "Bond";
// concatenate the two, separate with a space
var name = fname + " " + lname;
</script>
If your string contains quotes, carriage returns or
backslashes, it's necessary to escape these special characters with a backslash.
<script language="Javascript">
// will cause an error due to mismatched quotes
var movie = 'Baby's Day Out';
// will be fine
var movie = 'Baby\'s Day Out';
</script>
Try it out yourself and see, and then flip the page
to see how to dress up your strings.