Richard's JavaScript Tutorials

Updated 07/18/14


Formatting strings and using methods for string manipulation
Home
Animation
precachedImages
digitalClock
Object Tag (Audio)
Cookies.htm
Math_Object
Using CSS
The Style Object
Client_Info
customObject
functions
UsingArrays
comments
concatenation
documentwrite
ifelse
Loops
forms
Strings
variables

 



In this section I will demonstrate methods for manipulating strings. You can find a list of methods  for creating and using the String object at www.devguru.com


In my first demonstration I will use the charAt() method. This method will return the character at the location within the () for the charAt() method. For this example I will create a variable to use as a parameter for the charAt() method. This is what I call my backwards script, it uses the name variable to store the data I want to display backwards. Then I create a variable ("v") that will use the name variables length property to display the last character using the charAt() method. Finally I will subtract decrement variable v until there is no more characters left. Below is the script:

<script type="text/javascript">
<!--
var name = "Richard S. Vasquez";
// Backwards Script
for ( var v = (name.length - 1); v >= 0; i --)
{
document.write( name.charAt (v) ) ;
}
//--></script>

And here is the output:


Now lets try manipulating another string with the link() method. This method will return a string as a hyperlink. Below is the script:

<script type="text/javascript">
<!--
document.write( "Text for the link goes here".link("http://www.cayemay.com")) ;
// --> </script>


And here is the output for the above code:


You can always check out any of the other string data manipulation methods and use them in a similar manner. We will try one more here to give you one more example, then you can try creating your own JavaScript and manipulate a string using a string object method.

For this example we will use the toUpperCase() method. This string be converted to all upper case letters when used with this method. I also use another string method fontcolor to change the font of the testString variable to red. Here is an example of the code:

<script type="text/javascript">
<!--
var testString = "all small case letters"

// toLowerCase() method would do the opposite, convert the case of the entire string to lower case.
testString = testString.toUpperCase()
testString = testString.fontcolor("red")
document.write(testString);
// --> </script>

This script produces the following string:

Or you can get the same results by concatenating the 2 statements into one:

testString = testString.toUpperCase().fontcolor("red")


Now lets try formatting a string with the Italics(), fontcolor() and Big() methods.


<script type="text/javascript">
<!--
var italicsTest = "Look at my pretty scripts"

// Using Italics() and Big() method to convert the string
italicsTest = italicsTest.italics().big().fontcolor("blue")
document.write(italicsTest);
// --> </script>

Produces the following string:


Now try formatting string and using string object methods in your own JavaScript!

 

 

This site was last updated 07/08/14