06js字符串

字符串的声明

值类型赋值

var str = "abc";

new关键字赋值(应用了对应值地址)

var str = new String("abc");

es6新增 字符串模板

var x = "abc ";
var str = `${x}def`;
console.log(str);//abc def

 

new String()和String()的区别

new String是构建一 个String对象 (引用数据类型 有地址指向)

String单纯的将对应的传进入的参数转为string类型(值类型)

字符串的属性

length属性返回字符串的长度(包含空格及换行)

var str = "ab c";
console.log(str.length);//4

 

 通过下标来获取对应的字符串

var str = "ab c";
console.log(str[1]);//b

 

字符串的方法(函数)

var str = "abcde";
console.log(str.charAt(3));//获取下标为3的字符d
var str = "abcde";
console.log(str.charCodeAt(3));//获取下标为3的字符的编码(ASCII码)
//ASCII编码转换成字符 ASCII(American Standard Code for Information Interchange,美国标准信息交换代码) 
console.log(String.fromCharCode());
var str1 = "hello";
var str2 = str1.concat(" world");//字符串拼接
console.log(str2);//hello world

 

 字符串的查找方法

var str = "abcdabcd";
var index = str.indexOf("c");//查找字符串"c"从前往后找 找到则返回下标如果没找到则返回-1
console.log(index);//2

var str = "abcdabcd";
var index = str.lastIndexOf("c");//查找字符串"c"从后往前找, 找到则返回下标如果没找到则返回-1
console.log(index);//6

 

 

Math对象: Math对象可以用于执行数学任务

//Math对象的常用函数:
Math.round(3.6)     //四舍五入
Math.random()       //返回0-1之间的随机数
Math.max(num1, num2)   //返回较大的数
Math.min(num1, num2)   //返回较小的数
Math.abs(num)       //绝对值
Math.ceil(19.3)     //向上取整
Math.floor(11.8)    //向下取整
Math.pow(x, y)       //x的y次方
Math.sqrt(num)      //开平方

 

posted @ 2022-08-01 19:25  一歧日和  阅读(52)  评论(0)    收藏  举报