发布作者: JavaScript大王
百度收录: 正在检测是否收录...
最后更新: 2022年 12月 15日 13:48
作品采用: 《 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 》许可协议授权
// JavaScript允许使用\uxxxx形式表示字符串
console.log("\u0061"); // a
// 局限性:超过\uFFFF的字符需要用两个双字节的形式表示
console.log("\uD872\uDFB7"); // 𠮷
// ES6新特性:只要将码点放入大括号,就能正确识别
console.log("\u{20BB7}"); // 𠮷
// 大括号表示法等价于UTF-16编码
let res1 = "\u{1F680}" === "\uD83D\uDE80"; // true
console.log(res1);
const s = "韛";
let res1 = s.codePointAt(0);
String.fromCodePoint(0x20BB7);
// 利用码点创建一个字符串
let text = String.fromCodePoint(0x20BB7);
// for循环
for(let i = 0 ; i < text.length ; i++){
console.log(text[i]); // for循环认为text包含两个不可打印的字符
}
// for...of遍历
for(let str of text){
console.log(str); // 𠮷 for of循环能正确识别
}
语法:
/*
text:要查找的字符
pos:可选,表示查找开始的位置
*/
str.includes("text"[,pos]);
const str = "hello world";
// includes:查找参数字符串是否位于目标中
let res1 = str.includes("ll",0);
console.log(res1);// true
语法:
/*
text:要查找的字符
pos:可选,表示查找开始的位置
*/
str.startsWith("text"[,pos]);
// startsWith:查找字符是否位于目标的开头
let res2 = str.startsWith("w",6);
console.log(res2); //true
语法:
/*
text:要查找的字符
pos:可选,表示从pos之前的位置开始
*/
str.endsWith("text"[,pos]);
// endsWith:查找字符是否位于目标的末尾
let res3 = str.endsWith("o",8);
console.log(res3); // true
/*
n:重复的次数,可以是数值、字符串NaN
*/
str.repeat(n);
// 参数是字符串、小数、0~-1之间的数值、NaN、负数或Infinity时的返回值
// 1.参数为字符串==>会先将字符串转换为数值
const str = "peanut";
let res1 = str.repeat("3");
let res2 = str.repeat("haha");
console.log(res1); // peanutpeanutpeanut
console.log(res2); // 非数字的字符串返回空值
// 2.参数为小数===>向下取整
let res3 = str.repeat(2.9);
let res4 = str.repeat(2.4);
console.log(res3); // peanutpeanut
console.log(res4); // peanutpeanut
// 3.-1~0之间的数值===>先进行取整,取整后等于-0 === 0
let res5 = str.repeat(-0.5); // 取整运算后等同于0
console.log(res5); // 啥也没有
// 4.NaN===> 等同于0
let res6 = str.repeat(NaN);
console.log(res6); // 啥也没有
// 5.负数/Infinity===>报错
let res7 = str.repeat(-5);
let res8 = str.repeat(+Infinity);
let res9 = str.repeat(-Infinity);
语法:
/*
num:表示补全后的字符串长度;
text:用来补全的字符;
*/
padStart(num,text);
语法:
/*
num:表示补全后的字符串长度;
text:用来补全的字符;
*/
padEnd(num,text);
原字符串长度 >= 指定的最小长度:返回原字符串;
const str = "peanut";
let res1 = str.padStart(5);
let res2 = str.padEnd(5);
console.log(res1,res2); // peanut peanut
原字符串长度 < 指定的最小长度:按照指定字符补全不足的字符;
let res3 = str.padStart(10,"☹");
let res4 = str.padEnd(10,"-");
console.log(res3); // ☹☹☹☹peanut
console.log(res4); // peanut----
原字符串长度与补全字符长度之和 > 指定的最小长度:截去超出位数的补全字符串;
let res5 = str.padStart(8,"☹☹☹");
let res6 = str.padEnd(8,"---");
console.log(res5); // ☹☹peanut
console.log(res6); // peanut--
不指定用于补全的字符:默认使用空格补全;
let res7 = str.padStart(8);
let res8 = str.padEnd(8);
console.log(res7); // peanut
console.log(res8); // peanut
提示字符串格式;
// 1.补全数值位数
let num = "86";
let num_padS = num.padStart(4,"0");
let num_padE = num.padEnd(4,"0");
console.log(num_padS); // 0086
console.log(num_padE); // 8600
// 2.提示字符串格式
let data = "12";
let data_S = data.padStart(10,"YYYY-MM-12");
console.log(data_S); // YYYY-MM-12
// 1.在模板字符串中,使用反引号要转义
const str1 = `peanut \`run\` `;
console.log(str1); // peanut `run`
// 2.表示多行字符时,所有的换行和空格都会保留
const str2 = `peanut
like code!`;
console.log(str2); // peanut
// like code!
``.trim();
// 4.在模板字符串中嵌入变量,需要将变量写在${}中
for(let i = 0 ; i < 5 ; i++){
document.write(`这是第${i}次循环!`);
}
// 5.1 大括号可以是 任意表达式
const x = 1;
const y = 2;
console.log(`$ + ${y} = ${x + y}`); // 1 + 2 = 3
// 5.2 也可以进行运算
console.log(`${x+ y}`); // 3
// 5.3 引用对象属性
// 定义对象
const person = {
name : "peanut",
age : 33
}
console.log(`my name is ${person.name} , ${person.age} years old!`); // my name is peanut , 33 years old!
// 5.4 调用函数
function test() {
console.log("peanut 666");
}
`test text : ${test()}`; // peanut 666
// 写法1:
let str = "return" + "`hello ${name}`";
let fn = new Function("name",str);
console.log(fn("宝鸡"));
// 写法2:
let func = (name) => `Hello ${name}!`;
func('Jack') // "Hello Jack!"
—— 评论区 ——