Home » Online Computer Education » Learn Java Script, Writing Text with Strings, Lesson No. 15

Learn Java Script, Writing Text with Strings, Lesson No. 15

 
We have spent a lot of time already this morning learning about how to work with values. One object in particular, the String object, you will find yourself using over and over again in your scripts. I thought I’d spend a little time going over the String object now, even though the lesson on JavaScript and JScript objects isn’t until this later this morning. Consider this a sneak preview.

The String object is made up of a collection of text characters. You create an instance of a String object using the new keyword, as shown in the following example:

MyString = new String(“This is an example of a text string.”);
This statement creates a String object called MyString and assigns text to it. The new keyword is used to create objects. However, you are not required to use it. You can also create a String object by simply referring to it as in the following example:

MyString = “This is an example of a text string.”;
You can change the value assigned to a String object by assigning it a new value. The following example changes the value of the MyString string:

MyString = “This is a second example of a text string.”;
String Properties
Because a String object is a built-in JavaScript and JScript object, it has properties. In the case of the String object, there is just one property, its length. You can refer to an object’s property by typing the name of the object, followed by a period and the name of the property. In the case of the MyString object’s length property, you’d use MyString.length. The length property contains the number of characters in the string. The following example demonstrates how to display the value of this property, which is 40.

document.write(“The length of MyString is ” +
MyString.length);
String Methods
Although the String object has only one property, it has many methods. A method is an action that can be taken against the object. For example, you can use the String object’s bold() method to display the string in bold, as shown in this example:

document.write(MyString.bold());
When referencing an object’s methods, the correct syntax to use is the name of the object, a period, the name of the method, and the left and right parentheses. In this example, the parentheses did not contain anything. Some methods enable you to pass arguments to the method by placing them inside the parentheses.

For example, you can display strings in all uppercase characters using the toUpperCase() method:

document.write(MyString.toUpperCase());
Similarly, there is a toLowerCase() method. Other methods enable you to set the string’s font color and font size or to display it in italic, blinking, or strikethrough text. A particularly useful method is substring(). This method enables you to extract a portion of a string’s text.

For example, you might create a string that contains people’s names and addresses. You could reserve the first 15 character positions of the string for the person’s name, the next 20 characters for his street address, and the last 17 characters for the city, state, and ZIP code information. Below you will find three examples of this type of string.

cust1Info = “Bobby B Jones 9995 Park Place Ct Richmond VA 23232 “;
cust2Info = “Sue K Miller 1112 Rockford Lane Richmond VA 23232 “;
cust3Info = “Bobby B Jones 9995 Richland Drive Richmond VA 23223 “;
Now, using the String object’s substring() method, you can extract just the name portion of any string, as shown in the following example:

<HTML>
<HEAD>
<TITLE>Script 2.6 – Substring Method Example</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
var cust1Info = “Bobby B Jones 9995 Park Place Ct
Richmond VA 23232″;
var cust2Info = “Sue K Miller 1112 Rockford Lane
Richmond VA 23232″;
var cust3Info = “Bobby B Jones 9995 Richland Drive
Richmond VA 23223″;
document.write(cust1Info.substring(0,14),”<BR>”);
document.write(cust2Info.substring(0,14),”<BR>”);
document.write(cust3Info.substring(0,14),”<BR>”);
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>
1
String Concatenation
You can do many cool things with strings. For example, you can concatenate two or more of them together using the + operator, as shown in the following example:

<HTML>
<HEAD>
<TITLE>Script 2.7 – Example of String
Concatenation</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements
var stringOne = “Once upon a time, “;
var stringTwo = “there was a little house on a hill.”;
var stringThree = stringOne + stringTwo;
document.write(stringThree);
// End hiding JavaScript statements –>
</SCRIPT>
</BODY>
</HTML>
The script first establishes two strings, stringOne and stringTwo, and assigns them some text. Then it concatenates these two strings to create a new object named stringThree. Finally, the script displays the results in the browser window.
2

 

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published.