Find the Length of a String:
You can find the length of a String value by writing .length after the string variable or string literal.
console.log("Alan Peter".length);
The value 10 would be displayed in the console.
For example, if we created a variable const firstName = "Ada", we could find out how long the string Ada is by using the firstName.length property.
Use Bracket Notation to Find the First Character in a String:
Bracket notation is a way to get a character at a specific index within a string.
Most modern programming languages, like JavaScript, don't start counting at 1 like humans do. They start at 0. This is referred to as Zero-based indexing.
For example, the character at index 0 in the word Charles is C. So if const firstName = "Charles", you can get the value of the first letter of the string by using firstName[0].
Example:
const firstName = "Charles";
const firstLetter = firstName[0];
firstLetter would have a value of the string C.
Understand String Immutability:
In JavaScript, String values are immutable, which means that they cannot be altered once created.
For example, the following code:
let myStr = "Bob";
myStr[0] = "J";
cannot change the value of myStr to Job, because the contents of myStr cannot be altered. Note that this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed. The only way to change myStr would be to assign it with a new string, like this:
let myStr = "Bob";
myStr = "Job";
Use Bracket Notation to Find the Nth Character in a String:
You can also use bracket notation to get the character at other positions within a string.
Remember that computers start counting at 0, so the first character is actually the zeroth character.
Example:
const firstName = "Ada";
const secondLetterOfFirstName = firstName[1];
secondLetterOfFirstName would have a value of the string d.
Use Bracket Notation to Find the Last Character in a String:
In order to get the last letter of a string, you can subtract one from the string's length.
For example, if const firstName = "Ada", you can get the value of the last letter of the string by using firstName[firstName.length - 1].
Example:
const firstName = "Ada";
const lastLetter = firstName[firstName.length - 1];
lastLetter would have a value of the string a.
Use Bracket Notation to Find the Nth-to-Last Character in a String:
You can use the same principle we just used to retrieve the last character in a string to retrieve the Nth-to-last character.
For example, you can get the value of the third-to-last letter of the const firstName = "Augusta" string by using firstName[firstName.length - 3]
Example:
const firstName = "Augusta";
const thirdToLastLetter = firstName[firstName.length - 3];
thirdToLastLetter would have a value of the string s.
No comments:
Post a Comment