Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Stringing Things Along
Perform string manipulation tasks on the client with the JavaScript String object

| Elementary School |

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.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2017 Melonfire. All rights reserved
Terms and Conditions | Feedback