js字符串

字符方法

1.charAt()方法返回一单字符字符串的形式返回给定位置的那个字符。

var strValue="hello";
console.log(strValue.charAt(1);//"e";

2.charCodeAt()返回字符编码(ASCII)

var strValue="hello";
console.log(strValue.charCodeAt(1);//"101";
var str1="a";
console.log(str1.charCodeAt());//"97"

字符串操作方法

1.concat(),将一或多个字符串拼接起来(+号更简便易行)

var str1='hello';
    var str2=' bule';
    var str3=str1+str2;
    var str4=str1.concat(str2);
    console.log(str3);//hello bule
    console.log(str4);//hello bule

2.slice(),substr(),substring()返回一个子字符串,不影响原字符串。
接受两个参数,第一个参数为指定字符串开始的位置。第二个参数:slice和substring为指定的是字符串最后一个字符后面的位置,substr为指定的是返回的字符个数。

var str1='hello bule';
console.log(str1.slice(3));//'lo bule';
console.log(str1.substring(3));//'lo bule'
console.log(str1.substr(3));//'lo bule';

console.log(str1.slice(3,5));//'lo';
console.log(str1.substring(3,5));//'lo'
console.log(str1.substr(3,5));//'lo bu';

字符串位置

1.indexOf()从字符串的开头向后搜索,lastIndexOf()从字符串的末尾向前搜索

var str1='hello bule';
console.log(str1.indexOf('l'));//2
console.log(str1.lastIndexOf('l'));//8

trim()方法

这个方法会创建一个字符串的副本,删除前置及后缀的所有结果,然后返回结果。

var str1='     hello bule    ';
console.log(str1.trim());//"hello bule"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值