方法一:使用 slice()
方法
const str = "Hello World";
const lastChar = str.slice(-1);
console.log(lastChar); // 输出: "d"
方法二:使用 substring()
方法
const str = "JavaScript";
const lastChar = str.substring(str.length - 1);
console.log(lastChar); // 输出: "t"
方法三:使用 substr()
方法
const str = "Programming";
const lastChar = str.substr(-1);
console.log(lastChar); // 输出: "g"
方法四:使用数组索引
const str = "Example";
const lastChar = str[str.length - 1];
console.log(lastChar); // 输出: "e"
方法五:使用 charAt()
方法
const str = "Development";
const lastChar = str.charAt(str.length - 1);
console.log(lastChar); // 输出: "t"