1.字符串定义:
1.内置基本类型:(字面量)----用字面量定义字符串
var str = "heihei";//内置基本类型
console.log(typeof str);
console.log(str);
2.引用类型:(构造方法)
var str1 = new String("heihei");//引用类型
console.log(typeof str1);
console.log(str1);
2.ASCII码表<标准>
可以将字符与数字对应起来,在转为二进制。
a:97
A:65
字符0:48
空格:32
回车:13
3.字符串API
1.charAt:
功能:查询索引对应字符
参数:charAt(索引);
返回值:索引所对应字符
var str = "ahellolaowang";
console.log(str.charAt(1));//h
2.charCodeAt:
功能:查询索引对应字符,返回字符所对应的ascII码值
参数:charCodeAt(索引);
返回值:返回索引对应字符的asc码值
var str = "ahellolaowang";
console.log(str.charCodeAt(0));//97
3.length:
功能:字符串中元素的个数
参数:无
返回值:字符串的长度
var str = "ahellolaowang";
console.log(str.length);//13
4.fromCharCode:------通过String类型名来调用
功能:将asc码值转换为字符
参数:fromCharCode(asc1[asc2......ascn];
返回值:返回被转换的字符
console.log(String.fromCharCode(97,98));//ab
5.indexof:
功能:查找字符第一次出现的位置
参数:indexof(子串)
返回值:返回子串首字母的下标----------有则返回下标,无则返回-1.
var arr =[4,3,5,6,2,7,8,5];
console.log(arr.indexOf(5));//2
6.lastIdexof:
功能:查找字符最后一次出现的位置
参数:lastIdexof(子串)
返回值:有则返回下标,无则返回-1
7.replace:
功能:用参数2替换参数1---------只能替换第一次出现的字符
参数:replace(参数1,参数2)
返回值:被替换的字符串
var str = "jinyan de ge bi zhu zhe jinyan";
str = str.replace("jinyan","laowang");
console.log(str);//laowang de ge bi zhu zhe jinyan
8.slice:
功能:截取
参数:slice(起始位置,结束位置)------[左闭右开)支持负数
substring(起始位置,结束位置)-----------[左闭右开)
返回值:被截取的字符串
var str = "helloworld";
console.log(str.slice(2,5));//左闭右开 llo
console.log(str.substring(2,5));//左闭右开 llo
console.log(str.slice(-5,-2));//左开右闭 wor
9.split:
功能:字符串分隔符,将字符串转换为数组
参数:split("字符分隔符")
返回值:新的数组
10.toLowerCase
功能:大写转小写
参数:无
返回值:小写字符串
11.toUpperCase
功能:小写转大写
参数:无
返回值:大写字符串
console.log("heiHEI".toLowerCase());//heihei
console.log("heiHEI".toUpperCase());//HEIHEI