js字符串常用函数

来源:互联网 发布:孙俪的淘宝店铺 编辑:程序博客网 时间:2024/06/08 16:03

1.js处理字符串

1)length

var a = "hello";console.log(a.length);  //输出字符串长度

2)charAt
可返回指定位置的字符。

var str = "HELLO WORLD";var n = str.charAt(2);//返回的n 为 L

3)concat
用于连接两个或多个字符串,并返回新的字符串。

var str1 = "Hello ";var str2 = "world!";var n = str1.concat(str2);console.log(n);     //输出结果为Hello world!

4)indexOf
返回某子字符串在该字符串中第一次出现的位置,如果没有找到匹配的字符串则返回 -1。
备注:该方法区分大小写

var str="Hello world, welcome to the universe.";var n=str.indexOf("welcome");console.log(n);//输出结果为13//在字符串第五个位置开始查找字符 "e" 第一次出现的位置:var str="Hello world, welcome to the universe.";var n=str.indexOf("e",5);console.log(n);//输出结果为14

5)lastIndexOf
返回一个指定的字符串值最后出现的位置,如果指定第二个参数 start,则在一个字符串中的指定位置从后向前搜索。

注意: 该方法将从后向前检索字符串,但返回是从起始位置 (0) 开始计算子字符串最后出现的位置。
开始检索的位置在字符串的 start 处或字符串的结尾(没有指定 start 时)。
如果没有找到匹配字符串则返回 -1。

var str="I am from runoob,welcome to runoob site.";var n=str.lastIndexOf("runoob", 20);//开始检索位置在str[19]处,从后向前检索console.log(n); //输出从0开始,字符串第一次出现的位置,n = 10var m=str.lastIndexOf("runoob", 9);     //开始检索位置在str[8],从后向前检索console.log(m); //输出从0开始,字符串第一次出现的位置,m = -1

6)match
在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

备注:string.match(RegExp);
规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。
若RegExp对象有”g”标志,则说明是全局正则函数,将返回一个数组,其中存放了与它找到的匹配文本有关的信息。
若没有”g”标志,只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null。

var str = "The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/g);    //全局查找ain,将返回一个数组console.log(n);     //["ain", "ain", "ain"]var a = "hello";var b = ",world";// \w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。// + 匹配前面的子表达式一次或多次。var re = new RegExp(/^\w+$/);  //匹配一个或多个字符var is_alpha1 = a.match(re);console.log("is_alpha1:" + is_alpha1);      //is_alpha1:hellovar is_alpha2 = b.match(re);console.log("is_alpha2:" + is_alpha2);      //is_alpha2:nullvar re3 = new RegExp("\\w+","g");   //带有g标志,全局搜索var is_alpha3 = b.match(re3);console.log("is_alpha3:" + is_alpha3);      //is_alpha2:world

7)replace
用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

string.replace(searchvalue,newvalue);   \\用newvalue替换searchvalue//字符串替换var str = "Visit Microsoft!";var n = str.replace("Microsoft","Runoob");console.log(n); //Visit Runoob!//正则表达式替换var str1 = "Mr Blue has a blue house.";var re=str1.replace(/blue/gi,"red");    //全局搜索且不区分大小写console.log(re); //Mr red has a red house.

8)search
用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
返回值为数字,是与指定查找的字符串或者正则表达式相匹配的 String 对象起始位置。如果没有找到任何匹配的子串,则返回 -1。

var str="Visit Runoob!"; var n=str.search("Runoob");console.log(n); // n = 6(n 从0开始查找)

9)slice
slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
使用 start(包含) 和 end(不包含) 参数来指定字符串提取的部分。字符串下标从0开始。
提示:如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。

var str="Hello world!";var n=str.slice(1,5);console.log(n); // ello

10)split
用于把一个字符串分割成字符串数组。
string.split(seperator); //seperator作为字符串的分隔符
提示:该函数不改变原字符串。

var str="How are you doing today?";var n=str.split(" "); //以空格作为字符串分隔符console.log(n); //How,are,you,doing,today?//使用空字符串作为分隔符,那么 stringObject 中的每个字符之间都会被分割。var str="How are you doing today?";var n=str.split("");console.log(n); //H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?//使用Limit参数var str="How are you doing today?";var n=str.split(" ",3); //limit=3,输出数组的前3个值console.log(n); //How,are,you

11)substr
可在字符串中抽取从 开始 下标开始的指定数目的字符。
提示: substr() 的参数指定的是子串的开始位置和长度,该函数不改变原字符串

var str="Hello world!";var n=str.substr(2,3); //从第二个下标str[2]开始,提取3个字符console.log(n); //llo

12)substring
用于提取字符串中介于两个指定下标之间的字符。

string.substring(from, to); //to可省略,from(包含),to(不包含)var str="Hello world!";var sub1 = str.substring(3); //lo world!var sub2 = str.substring(3,7); //lo w

备注:substring() 和 slice()函数相似,但slice函数允许负数下标,表示从字符串尾部开始计算。

13)转换字符串大小写

string.toUpperCase(); //把整个字符串改为大写字母string.toLowerrCase(); //把整个字符串改为小写字母
原创粉丝点击