[JavaScript]
[Previous page] [Section contents page] [Next page]
Strings

We saw earlier that a string is really a built-in JavaScript object created when you set a variable equal to a string of characters enclosed in quotation marks. In many cases it will be useful to parse a string to obtain particular information from it, using some built-in methods for the string object.

First, note that that the quotation marks that enclose a string may be single or double, but that the beginning and ending marks must be of the same type. The ability to use single or double quotation marks is important, because it allows you to nest one string inside another (or to look at it in a more common-sensical way, to create a string that includes quotation marks). When you nest strings, however, remember that for each level of nesting you must alternate single and double quotation marks:

myString = "'the answer is "42",' she said"

You can combine strings with each other, or with variables, using the + operator. If you need a space between two strings, or between a string and a variable, remember that you have to include the space in a string>

Example

var yourBrowser = navigator.appName
var myMessage = 'Your browser is ' + yourBrowser
alert(myMessage)

Many of the pre-defined objects for JavaScript have methods. Some particularly useful ones are those associated with the string object. String objects are formed whenever a string is defined as the value of some variable. Many pre-defined object properties are also strings, though some may contain non-ASCII characters that can cause problems when you try to work on the string objects. You can avoid these problems by running such strings through the built-in unescape function before you use them. For example:

	// get current URL to plain ASCII
	var URL = unescape(location.href)
	

The properties and methods associated with the string object are as follows:

  • string.length // returns the number of characters in the string; characters are numbered from 0 - length
  • string.toLowerCase() // converts the string to lowercase
  • string.toUpperCase() // converts the string to uppercase
  • string.indexOf(search_string,start_index) // returns the position of the beginning of the search_string; optionally, you can specify where the search for the string should begin, which might be useful if it appears multiple times and you want to locate a specific instance
  • string.lastIndexOf(search_string,start_index) // similar to indexOf, but begins the search from the end of the string, instead of the beginning
  • string.charAt(index) // returns the character at the specified position -- counting from 0!
  • string.substring(indexA,indexB) // returns the characters starting at position indexA and ending at indexB

Below is a string obtained using the object property shown just above for the URL variable, and the succeeding lines show some of the string methods and what they return when applied to this string:

I determined the path and the file name above using a JavaScript that first gets the location.href object and then uses some of the string methods shown above to extract the file name from the string. Can you figure out how I did it? Give it a try; I'll show you the code for my version of the script later.

[Previous page] [Section contents page] [Next page]