JS get the last character of a string
Method 1: String.charAt ( String.length - 1)
const str = "123456";
console.log(str.charAt(str.length - 1)); // 6
Method 2: String.substring ( String.length - 1)
const str = "123456";
console.log(str.substring(str.length - 1)); // 6
JS intercepts the string after a specific character
indexOf() The index of the first occurrence of a character from left to right: String .indexOf("-")
const str = "2022-11-19";
console.log(str.substring(str.indexOf("-") + 1)); // 11-19
lastIndexOf() The index of the first occurrence of a character from right to left: String .lastIndexOf("-")
const str = "2022-11-19";
console.log(str.substring(str.lastIndexOf("-") + 1)); // 19
JS removes the first N characters in a string
Remove the first three digits of the string: String .slice(3)
const str = "12345678";
console.log(str.slice(3)); // 45678
JS removes the last N characters in the string
Remove the last two digits of the string: String .slice(0, -2)
const str = "12345678";
console.log(str.slice(0, -2)); // 123456
JS adds a specific string to the string
Insert a specific string after the 3rd digit of the string: String.slice(0, 3) + "newStr" + String.slice(3)
const str = "123456";
console.log(str.slice(0, 3) + "newStr" + str.slice(3)); // 123newStr456
JS intercepts the first N characters of a string
Intercept the first five digits of the string: String.substring(0, 5)
const str = "12345678";
console.log(str.substring(0, 5)); // 12345
JS intercepts N characters after the string
Intercept the last three digits of the string: String.substring(String.length - 3, String.length)
const str = "12345678";
console.log(str.substring(str.length - 3, str.length)); // 678
JS number to string
Method 1: Number.toString()
const num = 12345;
console.log(num.toString()); // 12345
Method 2 (number + any string): Number + ""
const num = 12345;
console.log(num + ""); // 12345
JS string to number
Method 1 (round down): parseInt(Number)
const num = "123.75";
console.log(parseInt(num)); // 123
Method 2 (reserve decimals): parseFloat(Number)
const num = "123.75";
console.log(parseFloat(num)); // 123.75
Method 3 (mandatory type conversion [reserve decimals]): Number(Number)
const num = "123.75";
console.log(Number(num)); // 123.75