js常用字符串函数

 1 // JS字符串
 2 //1.replace字符串替换,只能换第一部分,就是说多个字符相同,只能换下最先的
 3 var str='helloworld!';
 4 alert(str.replace('llo','9'));//he9world!
 5 
 6 //2.split字符串分隔符,把字符分成数组
 7 alert(str.split('llo'));//he,world! 数组
 8 
 9 //3.substr字符截取(起始位置0);
10 var str='¥11.60';
11 alert(str.substr(1));//11.60 从第一个位置开始截取到最后
12 alert(str.substr(-4,1));//1 从右边数第四个位置开始向右截取1个字符
13 //从第2个位置开始截取到最后。包括第二位置的字符
14 alert(str.substring(2));//1.60
15 //从第2个位置开始截取到第四位置截止(不包含第四位置)
16 alert(str.substring(2,4));//1.
17 
18 //4.通过字符获取编码
19 alert("a".charCodeAt(0));//97
20 //5.通过编码获取字符
21 String.fromCharCode(20013);//
22 
23 //6.获取字符串指定字符的位置
24 var str="http://www.234.com/admin/index.html";
25 str.indexOf("/");//5 第一个
26 str.lastIndexOf("/");//30 最后一个

 

posted @ 2016-08-17 10:23  落水的cx猿  阅读(140)  评论(0)    收藏  举报