The JavaScript String object provides many handy-dandy methods. Table 9-1 shows the most important of these string methods.
Besides the string methods shown in Table 9-1, the String object has one property, length. The length property contains the number of characters in a string.
Method | Description |
---|---|
charAt() | Returns the character at a given position in a string |
concat() | Concatenates one or more values to a string |
indexOf() | Searches a string for a substring or character; returns the first occurrence of the substring or character |
lastIndexOf() | Searches a string backwards for a substring or character; returns the first occurrence |
split() | Splits a string into an array of strings using a specified delimiter character to make the break |
substring() | Extracts a portion of a string |
toLowerCase() | Converts a copy of a string to all lower case |
toUpperCase() | Converts a copy of a string to all upper case |
In order to use one of these String object methods, as usual you need to create a string instance based on the object. You can do this in a number of ways. We’ve already used many of them throughout this book.
Methods for creating String objects include the following:
Using the String constructor, for example, var x = new String ("Hello");
Using the String() conversion function
Using an object’s toString method
Simply assigning a literal text string to a variable
Once you’ve created a string instance, you can use it to invoke a string method in the usual fashion (see Chapter 7, “ Working with Objects,” if you need a refresher course on how objects work in JavaScript).
To Use a String Object Method:
Create a variable containing a string. For example:
var myStr = "Beasts of England";
Use the dot operator to apply a string method to the string. For example:
var newStr = myStr.toLowerCase();
In the example, newStr would contain the string beasts of england.
You should also know that besides the methods shown in Table 9-1 there are a whole slew of methods that are used to make a copy of a string embedded in HTML tags. This makes sense because as a language JavaScript is widely used to output HTML. Table 9-2 describes these methods.
Method | Description |
---|---|
anchor() | Places a copy of the string within an HTML anchor |
blink() | Places a copy of a string within <BLINK> </BLINK> tags |
bold() | Places a copy of the string with <BOLD> </BOLD> tags |
fontcolor() | Uses the color attribute of the <FONT> tag to change the color in which the string is displayed |
fontsize() | Uses the size attribute of the <FONT> tag to change the font in which the string is displayed |
link() | Creates a hypertext link around a copy of the string |
Because these methods are pretty simple to figure out if you need to, and they’re not widely useful beyond using JavaScript to build pages for the Web, I won’t go into them in detail. However, I will show you a quick example so you can get the gist.
The following shows how you’d create an HTML hyperlink using the String object’s link method.
To Create and Display a Link:
Create a string. For example:
linkText = "Apress";
Use the link method of the String object to add a hyperlink to the string:
linkText.link("http://www.apress.com")
Display the results using the document.write method:
document.write(linkText.link("http://www.apress.com"));
Open an HTML page containing the code, as shown in Listing 9-1, in a browser. The link will appear like any hyperlink, with the URL it points to appearing in the browser’s status bar (see Figure 9-1).
Listing 9.1: Using the String Object’s Link Method
<HTML> <HEAD> <TITLE> A link is a link is a link... </TITLE> </HEAD> <BODY> <H1> <SCRIPT> linkText = "Apress"; document.write(linkText.link("http://www.apress.com")); </SCRIPT> </H1> </BODY> </HTML>
Figure 9-1: The String object’s link method is a quick way to create a hyperlink.
It’s time to move on and start having some fun playing with strings!