Apart from numbers, strings of text are the objects you are most likely to use in your scripts. As with numbers, there are two ways to define a string, one of which doesn't require an actual object declaration. Also as with numbers the difference between the two declarations is not something that will make a difference most of the time and so you may as well use the shorter declaration. Just about all of the times where the object version of a string is required the string will be converted automatically so that you don't have to worry about whether the text is actually an object or not. Here are examples of the two ways to define a variable containing text.
var text1 = "some text"; var text2 = new String("some more text");
Two text strings can be joined together (concatenated) using + between them like this:
var text3 = text1 + ' ' + text2;
Assuming the tree lines of code from the examples so far text3 contains some text some more text.
The string object supports quite a few methods for manipulating strings of text. The following are some of the more useful.
- charAt(number) selects the single character at the specified position within the string
- indexOf(substring) finds the position where the specified substring starts
- lastIndexOf(substring) finds the last occurrence of the substring within the string
- toLowerCase() convert the string to lowercase
- toUpperCase() convert the string to uppercase
- charCodeAt(number) selects the single character at the specified position within the string and returns the ASCII code equivalent
- concat(string2) concatenates string2 onto the end of the string
- fromCharCode(code) converts the ascii code back into the character that it represents, this is a static method not associated with any string
- localeCompare(string2) compares the string with string2 and returns -1, 0, or 1 depending on the sort sequence at the default locale
- match(regexp) searches for substrings within the string that match the regular expression and returns them in an array
- replace(regexp,string2) searches for substrings within the string that match the regular expression and replaces them with string2
- search(regexp) returns true if a substring within the string matches the regular expression
- split(char) splits the string at each occurrence of the specified character and puts them into an array (alternatively you can specify a regular expression instead of the char)
- substr(start,end) gets the specified part of the string
- toLocaleLowerCase() converts the string to lower case taking into account the default locale
- toLocaleUpperCase() converts the string to upper case taking into account the default locale
There are further methods other than those I have listed here but they are either ones that are so old that the functionality that they perform is now handled by stylesheets or they are so new that all browsers are yet to support them.
You will have noticed that a couple of the methods are static. What this basically means is that you use them with the base string object itself eg. String.fromCharCode(65) rather than using them with your own string eg. text1.charCodeAt(3) and 'some text'.toUpperCase() as the static methods are independent of the actual string you are processing in what they do.
While comparisons on numbers are done by comparing the size of the numbers themselves eg. 3 < 10, comparisons on text strings are done by comparing the ascii codes corresponding to the characters within the text starting with the left most character and processing until the result can be determined eg '10' < '3'.
More of This Tutorial
- 1 Hello World
- 2 Events
- 3 Visitor Triggered Events
- 4 Timed Events
- 5 Testing Conditions
- 6 Feature Sensing
- 7 Alternate Tag Location
- 8 Functions and Methods
- 9 Passing Parameters
- 10 Variables/Properties
- 11 Objects
- 12 Arrays
- 13 Nodelists
- 14 Loops
- 15 Numbers
- 17 Dates
- 18 Alert
- 19 Other Objects
- 20 Extending Objects
- 21 Creating Objects
- 22 Collision Proofing
- 24 Updating the Web Page