The following are examples of Handling Strings
Understanding String Objects
The string object provides properties and methods to get information about strings ot ot modify strings. A string object is created in either of two ways: a programmer creates one by using the new keyword with the constructor function, or JavaScript creates one temporarily when one of the methods is called from a string literal. Now you are probably wondering what make a String object and what makes a string literal.
One way to create a string object is to use the new keyword, as you've done with other objects previously. You would replace an instance_name with the name you want to use for the instance of the String object. So if you wanted to create an instance of the String object named guitar_string, you could use the following code:
var guitar_string= new String("G");
This script creates an instance of the String object for the string "G".
Although creating a String object with the new keyword can be useful in some ways, string literals are used more often.
You can create a string literal just by assigning a string vaule to a variable. This technique is a bit shorter then creating a String object using the new keyword and still aloows you to use all the methods of the String object (as well as the properties). A string literal is created in the code that follows. Notice that the code assigned a string value to a variable:
var guitar_string="G";
This makes the string "G" a string literal, which you know as a regular text string. With text strings, you're also allowed to use the properties and methods of the String object.
The best way to define this is:
Difference between a String object and String literal:
Two variables having the same string value will be equal.
Two String objects having the same value will not be equal.
When you opened this page, you had a pop-up window that stated "This is NASCAR". That was the prototype property. This is used to add properties or methods to String objects on the page.
Showing the Length Property String
In the line below, we are going to tell JavaScript to count the length of the string and return that number to us. This property returns the length of the string, whic is the number of characters contained in the string. The string is written var racing_string= new String("NEXTEL Cup Series"); which does indeed contain 17 characters.
Now we are going to play with some String Literal methods. These methods can range from changing the size of the text to the color of the text. There are actually 29 methods available for use. We will show .
First lets see a bold statement. By adding the bold() to the variable, we can change the appearance of the text with using HTML.
Now let do a few in a row. We will change the color of the text, use the concat method (which combines strings), change the size of the text, set some italics, and change the case. See if you can tell what is where!
Ok, so the breakdown goes like this. Line 1 is italics, line 2 is font color change, line 3 is font size change, line 4 is changing the text to Uppercase, line 5 is the concat method (combines 3 concat to the original variable) and the last line, if you are viewing this in Netscape, is BLINKING!
There are, of course, other things you can do with handling strings but this will give you a good start on future projects. And again, I add, JavaScript really isn't that bad!!!