
| String Objects are the most built-in of all the built-in
JavaScript Objects. You do not even use the key word new when
creating a String Object. Any variable whose value is a string is
actually a String Object. Literal strings such as "HelloWorld"
are also String Objects. (Examples)
String Objects have one Property, length, and many Methods. The length Property gives the length of the String. The methods fall into three categories: The following methods can be used on String Objects to access, control, or modify their content:
Displaying Subsets of Strings
The methods charAt and substring are used to extract either a single character from a string, at position index, or to extract a range of characters, from position from-index up to but not including position to-index. Character positions are zero-based, as are all JavaScript arrays, so that all indices must fall between 0 and one less than the length of the array. For example, using myStr, we have
These methods both return strings. Finally, both the indexOf and lastIndexOf methods are used to search for char with a string. indexOf searches from the beginning (left side) of the string and lastIndexOf searches from the end (right side). Both return an integer index if they find the character, and -1 if they do not. Using myStr again, we can search for the character o from both sides:
The first search finds the first a of the word "what" at position 12, and the second search finds the second a of the "happens" since that is the first a when searching from right to left. Both of these methods also take an optional second argument that specifies an initial index at which to start the search.
The code that produced the above anchor & appearance of the String:
The above example will be discussed in Methods that convert the String into an HTML element. The string appearance methods are used to control how a string appears when displayed on a Web page. If you are creating a page with standard HTML tags you would achieve the same effects by using various tags. For example, to make the string "help" appear in italics you would write <I>help</I>. The string appearance methods allow you to obtain the same effects in JavaScript without using the corresponding HTML elements. The string appearance methods are as follows: String Object Methods for HTML Formatting
The toLowerCase and toUpperCase methods convert the contents of the string entirely to lower - and uppercase, respectively. In addition, we can apply the two case conversion methods and get
These two functions do nothing to characters that have no case, so the two spaces in this string are unchanged. We could have also applied the methods directly to the literal form of this string object, so "Look At This".toLowerCase() is also equal to "look at this". Most of these methods should be self-explanatory. The italics method, for example, performs exactly the same function as the <I> tag in HTML. The only two that take arguments are the fontcolor and fontsize methods. The fontcolor method changes the font color of the string, as if the <FONT COLOR="color"> attribute had been size. Similarly, the fontsize method changes the size of the font used for displaying a string as if the <FONT SIZE="size"> attribute had been given. color should be a string; size may be a number or a string. If it's a number then this specifies an absolute font size; if it's a string such as "+2" it specifies an increment relative to the current font size. The listing below shows several examples using the string appearance methods.
String Methods Can Be Used to Change How Strings Are Displayed var bStr = "big"; /* This displays strings with both big and small text. */ document.write("<BR>This
is " + bStr.big() + "
text"); /* document.write("<BR>This
is <BIG>big</BIG> text"); JavaScript provides two string methods for converting strings into hypertext entities. These methods should be clearly distinguished from the HTML objects, such as forms, which are discussed in the section "Browser and HTML Objects". These methods are used to create HTML, while the HTML objects already are HTML. The two methods in this category are as follows: HTML String Methods
The listing below uses these methods and shows a simple example that sets up an anchor target and then links to it. String Methods Can Be Used to Create HTML Anchors and Links var strAppear = "String Appearance Methods"; document.write(strAppear.anchor(tarAppear)); produces the output below:
document.write("Click here to view " + strAppear.link(#" + tarAppear)); produces the output below:
|