js基础-字符串的方法

在底层,字符串是以字符数组的形式保存的

以下方法都不会影响原字符串!!!

var str = "hello world";

console.log(str.length);  //①返回字符串长度

var result = str[3];                //②返回字符串指定位置的字符
console.log(result);   

result = str.charAt(3);     //③返回字符串指定位置的字符
console.log(result);  

result=str.charCodeAt(3);  //④返回指定位置字符的unicode编码
console.log(result);

result = String.fromCharCode(96);  //⑤根据字符编码获取指定字符
console.log(result);

result = str.concat("连接字符串");      //字符串连接,不影响原字符串
console.log(result);

result = str.indexOf("l");    //从字符串开头开始往后找,返回该字符出现在字符串中的第一个索引位置,不存在该字符则返回-1
document.write(result);

result = str.indexOf("l",5);  //第二个参数,指定开始查找的位置
document.write(result);

result = str.lastIndexOf("l");    //从字符串末尾往前找,返回该字符出现在字符串中的第一个索引位置,不存在该字符则返回-1
document.write(result);

result = str.slice(1,2);     //截取字符串,[a,b)
result = str.slice(1);       //截取字符串,从索引位置[1开始截取到末尾
result = str.slice(1,-1);   //截取字符串,从索引位置1开始截取到倒数第一个
document.write(result);

result=str.substring(1,2); //和str.slice(1,2)类似,截取[a,b),不同的是:a>b时会调换a和b的位置,并且负数当成0。
str.substring(2,1) ==str.substring(1,2);
str.substring(2,-1)==str.substring(-1,2)==str.substring(0,2);

str.substr(2,3);  //截取字符串,第一个参数是索引位置,第二个参数是截取长度

var str = "123,dasd,eedsd,asd,fsg";
result = str.split(",");   //根据参数,拆分字符串为一个数组
console.log(result);

var str = "hello world";

console.log(str.length);  //①返回字符串长度

var result = str[3];                //②返回字符串指定位置的字符

console.log(result);   

result = str.charAt(3);     //③返回字符串指定位置的字符

console.log(result ); 

result=str.charCodeAt(3);  //④返回指定位置字符的unicode编码
console.log(result);

result = String.fromCharCode(96);  //⑤根据字符编码获取指定字符

console.log(result);


                                                           部分unicode字符编码


result = str.concat("连接字符串");      //字符串连接,不影响原字符串
console.log(result);

result = str.indexOf("l");    //从字符串开头开始往后找,返回该字符出现在字符串中的第一个索引位置,不存在该字符则返回-1
document.write(result);

result = str.indexOf("l",5);  //第二个参数,指定开始查找的位置

document.write(result);

result = str.lastIndexOf("l");    //从字符串末尾往前找,返回该字符出现在字符串中的第一个索引位置,不存在该字符则返回-1
document.write(result);

result = str.slice(1,2);     //截取字符串,[a,b)
result = str.slice(1);       //截取字符串,从索引位置[1开始截取到末尾
result = str.slice(1,-1);   //截取字符串,从索引位置1开始截取到倒数第一个
document.write(result);

result=str.substring(1,2); //和str.slice(1,2)类似,截取[a,b),不同的是:a>b时会调换a和b的位置,并且负数当成0。

str.substring(2,1) ==str.substring(1,2);

str.substring(2,-1)==str.substring(-1,2)==str.substring(0,2);

str.substr(2,3);  //截取字符串,第一个参数是索引位置,第二个参数是截取长度

var str = "123,dasd,eedsd,asd,fsg";

result = str.split(",");   //根据参数,拆分字符串为一个数组

console.log(result);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值