[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

javascript main
1. javascript basics
2. core javascript
3. js statements
4. js functions
5. js arrays
6. js objects
a. js obj basics
b. js obj instances
c. js obj classes
d. js obj methods
e. js obj prototypes
* f. js string object
g. js math object
h. js date object
7. debugging js
8. js client side
9. the js bom
10. js frames and windows
11. js forms
12. js regexp
13. js cookies
14. basic dhtml


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

The String Object

These tutorials are about JavaScript and its use for client-side Web programming.

The String Object

But wait, aren't strings primitive data types?

Yes they are. But JavaScript has an object equivalent of each of the primitive data types.

There are some functions that can be performed on the value of primitive data types that just require that they be objects in order to work properly. JavaScript, without batting an eyelash, calmly converts primitive data types into objects if it needs to, converting them back to primitive data types when it is done so that you never need know that anything happened. It is not a thing you should ever have to worry about, but it is worth knowing about because the String object comes with certain methods that can be used to perform various types of string processing.

The String object does have a constructor function and can be declared with it.

var aStr = new String('A string object');

However, JavaScript is flexible and you can declare a string with a simple assignment to a string variable and JavaScript will still let you treat it as an object.

var bStr = 'A string variable';

Regardless of how the string is created, you can still use it as an object or as a primitive data type. The standards fanatics may cringe at the lack of consistency, but it has its uses.

Before looking at some on the String methods, it should be noted that a string can be seen as an array of characters. As with arrays indexing starts with zero, so the character index positions number zero through length minus one.

1 1 1 1 1 1 1 Index Position: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Characters: A s t r i n g v a r i a b l e

Much string manipulation involves such things as searching the string, replacing text, deleting text, adding text. Since these require that you know the position where you are performing this action, you need to know how strings are indexed.

length

The only property of the String object that we are going to talk about here is length. Length is an instance property that stores the number of character positions that are in the string.

// the following will write 15 to the screen
var aStr = new String('A string object');
document.write(aStr.length);

// the following will write 16 to the screen
var bStr = 'A string variable';
document.write(bString.length);

Returning Individual Characters

JavaScript has two commands for returning individual characters from strings.

The charAt( ) method returns the character at the specfied index position. It takes a numeric value as an argument to specify the index position. It is an instance method. Which is to say it needs to work on a specific string.

// the following will write 'i' to the screen
// 'i' is the 6th character / 5th index position
var aStr = new String('A string object');
document.write(aStr.charAt(5));

The charCodeAt( ) method is similar to charAt( ); except it returns the decimal value of the character in the Latin-1 character set (ASCII).

// the following will write 101 to the screen
// 'e' is the 13th character / 12th index position
// lower-case 'e' is 101 in ASCII code
var aStr = new String('A string object');
document.write(aStr.charAt(12));

On the other hand, if you have a set of character codes that you want to convert to a string, there is also the fromcharCode( ) method. fromcharCode( ) converts the comma separated lsit of decimal character values into a string of characters. It is a static, or class method, which means it is not associated with a string, but rather the String keyword. It returns a value which is a string and must be assigned to some variable or otherwise processed.

// the following code will write 'Hello!'
var cStr = String.fromCharCode(72,101,108,108,111,33);
document.write(cStr);

Finding Sub-Strings

You can use the indexOf( ); and lastIndexOf( ); methods to search for the location of a sub-string within a string. Both take two arguments:

  • a text string that represents the sub-string you are looking for, and
  • an optional numeric value representing the character index position to start searching from.

The only real difference between the two is that indexOf( ); starts searching from the beginning of the string, while lastIndexOf( ); starts searching backwards from the end of the string. In both cases the method returns the index position of the first character of the sub-string. If the sub-string is not found, then they return -1.

JavaScript string searches are case sensitive so the following will assign -1 to the testPos variable.

someStr = 'This is a Cat';
testPos = someStr.indexOf('cat');

Whereas the following will return the value of 10:

someStr = 'This is a cat';
testPos = someStr.indexOf('cat');

If you are concerned about case, there are conversion methods to convert string to upper and lower case. This allows you to search without worrying about case. It does modify the string it is applied to. If you want to preserve the original string you should make a copy before changing the case.

The method to change to uppercase is toUpperCase( ). The method to change to lowercase is toLowerCase( ). They take no arguments.

someStr = 'This is a Cat';
// the following will write out 'THIS IS A CAT'
document.write(someStr.toUpperCase();
// the following will write out 'this is a cat'
document.write(someStr.toLowerCase();

Copying Substrings

You can also use the substr( ) and substring( ) methods.

The substring( ) code is the older method and takes two arguments. The starting index position of the substring and the ending index position of the string. The ending index position is not part of the extracted string.

substr( ) is the newer method and takes the first index position of the substring and its length. It is a little easier to work with, but is not supported on older browsers.

Both return a value that is the substring requested.

[top]