Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the last character of a string?

Tags:

javascript

How to get the last character of the string:

"linto.yahoo.com." 

The last character of this string is "."

How can I find this?

like image 391
Linto P D Avatar asked Oct 07 '10 18:10

Linto P D


1 Answers

An elegant and short alternative, is the String.prototype.slice method.

Just by:

str.slice(-1); 

A negative start index slices the string from length+index, to length, being index -1, the last character is extracted:

"abc".slice(-1); // "c"; 
like image 63
Christian C. Salvadó Avatar answered Oct 04 '22 16:10

Christian C. Salvadó