Strings as objects
The table below lists string methods 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 substring 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
property gives the number of characters in a string.
Caution!
Remember that the first position in a string is 0.
Note:
Hints:
- undefined
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
var myString = "This is my string.";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX