Stringing Things Along - Bigger, Bolder, Better (
Page 7 of 7 )
Finally, if you're looking to perform a little cosmetic surgery on your strings,
a good place to start is the toLowerCase() method, which comes in handy if you
need to convert a string into entirely lower-case characters (there's also a
toUpperCase() method, in case you change your mind)
<script language="JavaScript">
// set string
var str = "Jack Frost";
// returns "jack frost"
alert(str.toLowerCase());
// returns "JACK FROST"
alert(str.toUpperCase());
</script>
If you use HTML (I assume you do,
otherwise you wouldn't have gotten this far), you'll be happy to hear that
JavaScript provides a number of string formatting options equivalent to HTML's
formatting tags. These options as exposed as methods of the String object.
Here's an example which demonstrates:
<script language="Javascript">
// create a String object
var myStr = new String()
myStr = "Look, Ma, no hands";
// write it as is
document.write(myStr);
document.write("<br>");
// make it bolder...
document.write(myStr.bold());
document.write("<br>");
// ...bigger..
document.write(myStr.big());
document.write("<br>");
// ...more violent
document.write(myStr.strike());
document.write("<br>");
</script>These methods allow developers to
alter text formatting at runtime within the client, rather than with server-side
code - this is good for a number of reasons, including the fact that user
interface and presentation logic now lie in the domain of the client rather than
the server.
Wanna use all those functions together? Sure!
<script language="Javascript">
// create a String object
var myStr = new String()
myStr = "Look, Ma, no hands";
// all at once
document.write(myStr.bold().big().strike());
</script>
JavaScript also comes with a bunch of
other methods that allow you to alter the formatting of a string - play with
them on your own time and see what they can do:
blink() - create a
blinking string;
fixed() - create a string with a fixed-width
font;
fontcolor() - set the font colour;
fontsize() - set the font
size;
sub() - subscript the string;
sup() - superscript the
string;
And that's about it. I hope you enjoyed this article, and that it
offered you some insight into the string processing power at your disposal in
JavaScript. Now, get out there and practice!
Note: All examples in this
article have been tested on Internet Explorer 5.5 for Windows 98. Examples are
illustrative only, and are not meant for a production environment. Melonfire
provides no warranties or support for the source code described in this article.
YMMV!