字符串常用API
String.prototype.slice[start, end) // 字符串分割(截断)
- start--- 从第几位开始截
- end --- 截到第几位
// 举个栗子
let userName = '码农哥哥'
let handelUserName = (userName) => {
let firstName = userName.slice(0, 2); // 码农
let lastName = userName.slice(2, userName.lenght); // 哥哥
return [firstName, lastName]
}
let [firstName, lastName] = handelUserName(userName); // ['码农', '哥哥']
console.log(`${firstName}-${lastName}`); // 码农-哥哥
// slice也可以对数组进行截取
/*
* Array.prototype.slice
* start--- 从第几位开始截
* end --- 截到第几位
*/
let nums = [1, 2, 3, 4, 5, 6];
nums.slice(0, 3); // [1, 2, 3]
标签:实战,userName,slice,firstName,---,API,let,字符串,码农
From: https://www.cnblogs.com/bingquan1/p/16895069.html