收藏 分销(赏)

JavaScript的String字符串对象常用操作总结.doc

上传人:精*** 文档编号:9736180 上传时间:2025-04-05 格式:DOC 页数:3 大小:23KB 下载积分:5 金币
下载 相关 举报
JavaScript的String字符串对象常用操作总结.doc_第1页
第1页 / 共3页
JavaScript的String字符串对象常用操作总结.doc_第2页
第2页 / 共3页


点击查看更多>>
资源描述
  String对象用于存储字符串数据,这里我们做了JavaScript的String字符串对象常用操作总结,需要的朋友可以参考下   创建String对象方式   声明:String对象的方法也可以在所有基本字符串值中访问到。   调用构造函数String():   var str = new String();   var str = new String('hello world');//初始化str,str.length = 11;   String访问及查找的方式   1.访问(通过索引)   (1)charAt()或[]   1个参数,参数为字符位置,返回字符   var strValue = new String('hello world');   console.log(strValue.charAt(1));//e   console.log(strValue[1]);//e,IE7及以下版本使用这种方式,会返回undefined   (2)charCodeAt()   1个参数,参数为字符位置,返回字符编码   var strValue = new String('hello world');   console.log(strValue.charCodeAt(1));//101   2.查找位置   (1)indexOf()   第一个参数为指定子字符串,第二个参数为检索位置。返回索引,如果没有找到则返回-1   var str = 'hello world'   str.indexOf('l');//2,返回找到的第一个字符的位置   str.indexOf('l',6);//9   (2)lastIndexOf()   与indexOf()的区别在于,lastIndexOf()是从字符串的末尾向前搜索子字符串   字符方法   1.扩展字符串   concat()   接受任意数量参数,用于将一个或多个字符串拼接起来,返回拼接得到新的字符串副本。   var str = new String('hello');   var result = str.concat(' world');   console.log(result);//hello world   typeof result//"string"   2.获取子字符串方法   slice(),substr(),substring(),这三个方法都会返回被操作字符串的子字符串副本,而且也都接受1或2个参数,前闭后开[)   (1)slice()   var str = 'hello';   str.slice(0,2);//"he",第一个参数指定字符串开始的位置,第二个参数表示字符串到哪里结束   str.slice(-3);//"llo",o代表-1,依次倒数,-3代表倒数第三个的l   str.slice(-2,-1);//"l",同理,-2代表倒数第二个l,-1代表倒数第一的o   (2)substring()   var str = 'hello';   str.substring(0,2);//"he",此时的参数意义同str.slice(0,2)   str.substring(-3);//"hello",substring()方法会把所有负值参数转换为0   str.substring(-3,-2);//"",同上   (3)substr()   var str = 'hello';   str.substr(1,2);//"el",第一个参数指定字符串的开始位置,第二个参数指定的则是返回的字符个数   str.substr(-3);//"llo",此时的参数意义同str.slice(-3)   str.substr(-3,-1);//"",substr()方法会将负的第二个参数转换为0   substr()方法传递负值时在IE中存在问题,它会返回原始的字符串,IE9修复了这个问题   3.将字符串转换为数组   split()   基于指定的分隔符(可以是字符串,也可以是RegExp对象)将字符串分割成多个子字符串,并将结果放在一个数组中,可接受可选的第二个参数,用于指定数组的大小,返回数组。   var color = 'blue,red,orange';   color.split();//["red,blue,orange"],长度为1   color.split(',');//["blue", "red", "orange"],长度为3   var color = 'blue-red-orange';   color.split('-');//["blue", "red", "orange"],长度为3   color.split(',',2);//["blue", "red"]   4.字符串大小写转换   toLowerCase(),toUpperCase()   var str = 'hello';   str.toUpperCase();//"HELLO"   str.toLowerCase();//"hello"   5.删除字符串空格方法   trim()   删除字符串中前置以及后缀的所有空格,然后返回结果副本。   var str = ' hello world ';   str.trim()//"hello world"   6.字符串的模式匹配方法   (1)match()   参数:只接受一个参数,要么是一个正则表达式,要么是一个RegExp()对象。   返回:数组。数组中的第一项是与整个模式匹配的字符串,之后的每一项(如果有)保存着正则表达式捕获组匹配的字符串   本质上与调用exec()相同。   var text = 'cat, bat, sat, fat';   var pattern = /.at/;   var matches = text.match(pattern);   matches // ["cat"]   matches.input // "cat, bat, sat, fat"   matches.index // 0   (2)search()   参数:与match()方法相同。   返回:字符串中第一个匹配项的索引,如果没有匹配项,则返回-1。   search()方法始终从前向后找   var text = 'cat, bat, sat, fat';   var pattern = /at/;   text.search(pattern) // 1   (3)replace()   参数:接收两个参数,第一个参数可以是一个RegExp对象或者一个字符串(这个字符串不会转换成正则表达式),第二个参数可以是一个字符串或者一个函数。   如果 第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局标志(g)标志。   如果 第二个参数是字符串,那么还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中。   也可以是函数,传递给函数的参数依次是模式的匹配项,模式的匹配项在字符串中的位置,和原始字符串。在正则表达式定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项,第一个捕获组的匹配项,以此类推,但最后两个参数分别是模式的匹配项在字符串中的位置和原始字符串。   var text = 'xxx-love-xxx';   var pattern = /xxx/g;   var result = text.replace(pattern,'2')   result// "2-love-2"   text.replace(/(xxx)-\w{4}-(xxx)/g,'I love YOU');//"I love YOU"   var text = 'xxx-love-xxx';   var pattern1 = /xxx/g;   var result = text.replace(pattern1,'$$')   result// "$-love-$"   var result = text.replace(pattern1,'$&2')   result//"xxx2-love-xxx2"   var result = text.replace(pattern1,'$\'')   result//"-love-xxx-love-"   
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服