. Advertisement .
..3..
. Advertisement .
..4..
This article will show you how to get the last character of string Javascript. From that, you can find it easy to compile your code and deal with issues in Javascript.
How to get the last character of string javascript?
Before you find out how to get the last character of a string in Javascript, we will mention the string in Javascript.
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’.
Here are the main methods that help you get the last character of string Javascript.
Solution 1: Use the split() function
Syntax:
Split(separator,limit)
Parameters:
- 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]);
Output:
y
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]);
Output:
y
Solution 3: Use charAt()
Syntax:
charAt(position)
Parameter:
- Position: the index position of a character.
Javascript strings bring one inbuilt method known as “charAt.” The method shows the “index value” and returns the character at this “index.”
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:
Output:
y
Solution 4: Use Substr(-1) function
Syntax:
Substr(start, length)
Parameters:
- 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));
Output:
y
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:
Output:
y
Solution 5: Use Slice(-1) function
Syntax:
Slice(start, end)
Parameters:
- 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));
Output:
y
Conclusion
That’s all about typical ways that make you gain a deeper insight into how to get the last character of string Javascript. Finally, you can leave your feedback below this blog.
Read more:
Leave a comment