Strings as objects
The table below lists string methodsA function associated with a class. by name and what is returned. Notice that the string itself is not changed. Negative numbers are used for positions counting from the end of the string, with -1
for the last position, -2
for the second-to-last position, and so on.
Name | Meaning |
---|---|
.slice(start, stop) |
characters in positions start through stop - 1 |
.indexOf(sub) |
first position where substringA string formed by removing zero or more characters from the beginning of a string and zero or more characters from the end of a string. sub is found or -1 if it is not present |
.charAt(index) |
character at position index if index is a valid position in the string |
.toUpperCase() |
a copy of the string with each lower-case letter replaced by the corresponding upper-case letter |
.toLowerCase() |
a copy of the string with each upper-case letter replaced by the corresponding lower-case letter |
.replace(old, new) |
a copy of the string with the first copy of substring old replaced by substring new |
As mentioned earlier, the length
propertyA slot for one of the values bundled in an object. gives the number of characters in a string.
Caution!
Remember that the first position in a string is 0.
Note:
Hints:
- undefined
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
var myString = "This is my string.";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX