LILLY ROBOT |
A string is an object it has several properties and methods. A string object contains characters such as numerals, letters and symbols. The characters can be in single or double quotation marks.
A string can also be created using a string literal.
<script>
var name="Lilly Robot";
document.write(name);
</script>
Displays
An instance of a String object can be created using the key word new and the String constructor. In the Object lesson you will learn more about objects and the constructor.
var variableName=new String(argument)
Displays
A string has the following properties
In this lesson we are only going to look at the length property. You will learn more about the constructor and prototype in the Objects sections.
The length property returns the total number of characters in a string as an integer. This includes any space between the words. To access the length property we have to use a dot after the name of the object.
Remember! A String is an object.
nameOfObject.property
<script>
var myName="Lilly Robort";
document.write("The Length is " + myName.length);
</script>
Displays
Methods are used when you wnat to carry out certain tasks. The object string has several methods.
Returns the character at the given index. The first letter index is [0].
Displays
Returns the unicode value of the character at the given index in a string.
Displays
Joins two or more strings. Let us join the first name Lilly with the surname Robot and give it a new variable name called fullName.
Displays
You can concat a number of strings for example firstString.concat(secondString, thirdString...).
Displays
Searches for the character or substring from the begining of the string and returns the index of the first occurrence of the searched character or substring; if not found returns a -1.
Note: Index starts at [0].
Displays
You can specify a posistion in the string where you want the search to start from.
Displays
Searches for the character or substring from the end of the string and returns the index of the last occurrence of the searched character or substring; if not found returns -1.
Note: Index starts at [0].
Displays
Converts any lower case text to upper case text.
Displays
Converts lower case text to upper case text.
Displays
Searches a string for a match based on a regular expression - returns an array if there is a match and if there is no match returns null.
Displays
The slice() method slices a part of the string - it has a start index and an end index. The end index is optional, if it is not included then it will go up to the end of the string. The slice() method will accept negative indexes.
Displays
Note: The end index is up to 4, so it does not include the character v.
The substring() method returns the characters in a string between the from and to index; it does not accept any negative indexes.
Displays
Note: The end index does not include the last character r.
The substr() method returns the characters in a string from start to the length of the string. It is similar to the slice method(); the only difference is the second parameter in the substr() method is a length.
Displays
The spilt() method converts the string to an array, based on the delimiter and limit. The limit is an integer and it is optional; it specifies the maximum number of elements to be returned.
Displays
The replace() method searches the string and replaces a text with another text.
Displays
The above string contains the number 32 at the begining and at the end but the replace() method only found and replaced the 32 at the begining. Hence, if we wanted all the 32's replaced with 89 then it would be best to use the global regular expression match.
Displays
Searches for a match between a regular expression and a string. If found it returns the index of the match or -1 if not found.
Displays