JS day07—字符串

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值