In JavaScript, you can use bracket notation to access a specific character in a string by its index. The index of the first character in a string is 0, the second character is 1, and so on. To find the last character in a string, you can use the length property of the string, which returns the number of characters in the string.
Here is an example of how to find the last character in a string using bracket notation:
let str = "Hello, world!"; let lastChar = str[str.length - 1]; console.log(lastChar); // Output: "!"
[/dm_code_snippet]
In this example, the variable str
is assigned the string “Hello, world!” and the length property of the string returns the value 13. By subtracting 1 from the length of the string, you get the index of the last character, which is 12. Using bracket notation, you can access the character at this index, which is “!”.
Another way to find the last character in a string is using String.prototype.slice()
let str = "Hello, world!"; let lastChar = str.slice(-1); console.log(lastChar); // Output: "!"
[/dm_code_snippet]
.slice(-1) will slice the last letter of the string, giving you the last character as output.
You can also use the charAt() method to find the last character in a string:
let str = "Hello, world!"; let lastChar = str.charAt(str.length - 1); console.log(lastChar); // Output: "!"
[/dm_code_snippet]
The charAt() method takes an index as an argument and returns the character at that index. In this let str = “Hello, world!”; let lastChar = str.charAt(str.length – 1); console.log(lastChar); // Output: “!”example, we pass in str.length - 1
as the argument, which gives us the index of the last character in the string.
If you are working with Unicode strings and need to support characters from a wide range of scripts, it’s important to note that the length of a string does not always correspond to the number of visible characters. For example, combining characters such as diacritical marks, or certain East Asian characters, called ideographs in unicode are represented by two code units (surrogate pairs), but are seen as a single grapheme.
So it is safe to use the string.codePointAt(string.length – 1) instead.
let str = "Hello, world!"; let lastChar = String.fromCodePoint(str.codePointAt(str.length - 1)); console.log(lastChar); // Output: "!"
[/dm_code_snippet]
This way you can get the last unicode code point and convert it to a character.
There are a few other things you can do with strings in JavaScript.
- The
split()
method can be used to split a string into an array of substrings. For example, you can use it to split a sentence into words.
let str = "Hello, world!"; let words = str.split(" "); console.log(words); // Output: ["Hello,", "world!"]
[/dm_code_snippet]
- The
replace()
method can be used to replace a specific substring with another string. For example, you can use it to replace all occurrences of a specific word in a string.
let str = "Hello, world!"; let newStr = str.replace("world", "universe"); console.log(newStr); // Output: "Hello, universe!"
[/dm_code_snippet]
- The
toUpperCase()
method can be used to convert a string to uppercase, and thetoLowerCase()
method can be used to convert a string to lowercase.
let str = "Hello, World!"; console.log(str.toUpperCase()); // Output: "HELLO, WORLD!" console.log(str.toLowerCase()); // Output: "hello, world!"
[/dm_code_snippet]
- The
indexOf()
method can be used to find the position of the first occurrence of a specific substring in a string, or -1 if the substring is not found.
let str = "Hello, world!"; let index = str.indexOf("world"); console.log(index); // Output: 7
[/dm_code_snippet]
Keep in mind that these are only a few examples of the many built-in JavaScript string methods available. There are many more you can use to manipulate and work with strings in JavaScript.