Strings have stored a series of characters. Every character of a string might be used by its index. Next, the index begins at ‘0’ and ends at ‘length-1’ in which ‘length’ is the string’s length. When it comes to the first character, it can be ‘0’, and the last character is ‘length-1’.
Solution 1: Use the split() function
- Separator: The pattern defining where every split might happen.
- Limit: The non-negative integer limits the pieces to split the provided string.
The split() function allows you to split a string into different substrings. The function divides the string up to the delimiter, which we move as the parameter. When it comes to obtaining the final character of a string, you might use “ ” (empty character) as a delimiter and have the characters independently in an array.
Next, you should obtain the final element of the array. To do it, you must use the array[array.length], the popularly used array syntax to get the element at a provided index from your array.
var strTest = "wendy"; console.log(strTest.split("")[strTest.length - 1]);
Solution 2: Use the bracket notation
To get the last character of a string, you need to apply the bracket notation to use the string at the last index. As usual, indexes are zero-based, so the final character’s index is ‘str.length – 1’.
var strTest = "wendy"; console.log(strTest[strTest.length-1]);
Solution 3: Use charAt()
- Position: the index position of a character.
To obtain the final character of a string, you need to move ‘length – 1’ to that method parameter. ‘Length’ is the string length, and you could acquire it by reading the ‘.length’ attribute of the string.
Here is an example:
let strTest = 'hello wendy'; console.log(strTest.charAt(strTest.length - 1))
Finally, run your program, and the output is:
Solution 4: Use Substr(-1) function
- start: first character index. In the other words, the character begins extracting the substring.
- length: the size of substring we want, or the number of characters to contain in the last substring.
Substr(-1) resembles function substring(). Thus, you could apply either of those functions to obtain a string’s portion up to the values. Substr() approves double parameters. When it comes to acquiring the final character, pass -1 as the parameter as displayed by the code below.
var strTest = "wendy"; console.log(strTest.substr(-1));
On top of it, the substring() function can draw the same result. To obtain the final character, you determine the starting and end index as the string’s length.
Here is an example to make you understand further:
var strTest = "wendy"; console.log(strTest.substring(strTest.length-1, strTest.length));
Run the code, and the output is:
Solution 5: Use Slice(-1) function
- start: the index to begin the slicing. The index is zero-based.
- end: the index to end the slicing. It is the optional value, and the slice might get it as the string’s end if it is not given. The index is zero-based.
The slice() function is even a popularly used solution to work with strings. All the functions take double parameters, such as the start and end index.
Next, they are not similar to their behavior as slice (-1) provides the final character of a string. Now, let’s take a look at the example below.
var strTest = "wendy"; console.log(strTest.slice(-1));