Answers for "how to get last character in string in javascript"

30

javascript get last character of string

var str = "Hello";
var newString = str.substring(0, str.length - 1); //newString = Hell
Posted by: Guest on July-23-2019
0

JavaScript the last word of a string

// Get the last word of a string.

let str = "What I hate most in the world is fanaticism.";

// 1) The slice() method: without Considering the punctuation marks
str.slice(str.lastIndexOf(' ')); // => 'fanaticism.'

// 2) the spit() method():
let arr = str.split(' ');
arr[arr.length - 1]; // => 'fanaticism.'


// Considering the punctuation marks at the end of the string
str.match(/\b(\w+)\W*$/)[1]; // => 'fanaticism'
Posted by: Guest on February-02-2022

Code answers related to "how to get last character in string in javascript"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language