string类型的方法

string类型的方法

1. 字符方法

  • charAt(index)  

返回值:返回指定位置的字符

var str = 'java';

console.log(str.charAt(2));//v

  • charCodeAt(index)

返回值:以Unicode编码形式返回指定位置的字符

var str = 'java';

console.log(str.charCodeAt(2));//118

2. 字符串操作方法

  • concat方法

参数:要拼接的字符

返回值:返回拼接好的字符串

var str = 'java';

var str1 = 'js';

var str2 = 'html';

var res = str2.concat(str1,str);

console.log(res,str);//htmljsjava java

  • slice方法

参数:(start,end)

返回值:从start位置开始截取字符串到end(不包括end位置)

var str = 'javahtmlcss';

var res = str.slice(0,3);

console.log(res,str);//jav javahtmlcss

  • substring方法

参数:(start,end)

返回值:从start位置开始截取字符串到end(不包括end位置)

var str = 'javahtmlcss';

var res = str.substring(0,4);

var res1 = str.substring(0);

console.log(res,str);//java javahtmlcss

console.log(res1,str);//javahtmlcss javahtmlcss

  • substr方法

参数:(start,length)

返回值:从start位置开始截取长度length得字符串

var str = 'javahtmlcss';

var res = str.substr(0,2);//ja javahtmlcss

var res2 = str.substr(-3,2);//cs javahtmlcss

console.log(res,str);

console.log(res2,str);

3. 字符串位置方法

  • indexOf方法

参数:(查找的字符,index(可选))

返回值:返回第一次查到的字符索引或-1

var str = 'hello world';

var res = str.indexOf('o');

var res1 = str.indexOf('o',5);

var res2 = str.indexOf('m');

console.log(res,str);//4 hello world

console.log(res1,str);//7 hello world

console.log(res2,str);//-1 hello world

  • lastIndexOf方法

参数:(从后往前查找的字符,index(可选))

返回值:返回第一次查到的字符索引或-1

var str = 'hello world';

var res = str.lastIndexOf('o');//7 hello world

var res1 = str.lastIndexOf('o',2);//-1 hello world

var res2 = str.lastIndexOf('o',3);//-1 hello world

var res3 = str.lastIndexOf('o',6);//4 hello world

console.log(res,str);

console.log(res1,str);

console.log(res2,str);

console.log(res3,str);

4. 字符串大小写转换方法

  • toUpperCase:将字符串字符全部转为大写

var str = 'java html css';

var res = str.toUpperCase();

console.log(res);//JAVA HTML CSS

  • toLowerCase 将字符串字符全部转为小写

var str = 'JAVA HTML CSS';

var res = str.toLowerCase();

console.log(res);//java html css

5. 字符串的模式匹配方法

var str = 'hello';

console.log(str.match('l'));//[ 'l', index: 2, input: 'hello', groups: undefined ]

console.log(str.replace('l','L'));//heLlo

console.log(str.search('o'));//4

console.log(str.split());//[ 'hello' ]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值