字符串(String)操作

用于访问字符串中特定字符的方法
(1)charAt()方法以单字符字符串形式返回给定位置的那个字符:

var stringValue="hello World!";
console.log(stringValue.charAt(2));//"l"
console.log(stringValue[2]));//"l"

(2)charCodeAt()方法以字符编码形式返回给定位置的那个字符:

var stringValue="hello World!";
console.log(stringValue.charCodeAt(2));//"108"

fromCharCode()与charCodeAt()是相反的操作

console.log(String.fromCharCode(104,101,108,108,111));//"hello"

用于将一个或多个字符串拼接起来(concat)

var stringValue="hello";
var result1=stringValue.concat(" world");
var result2=stringValue.concat(" world","!");
console.log(result1);//"hello world"
console.log(result2);//"hello world!"
console.log(stringValue);//"hello"

基于子字符串创建新字符串(substring(),slice(),
substr()),不会修改原字符串的值。

var stringValue="hello world";
console.log(stringValue.slice(3));//"lo world"
console.log(stringValue.substring(3));//"lo world"
console.log(stringValue.substr(3));//"lo world"
console.log(stringValue.slice(3,7));//"lo w"//含头不含尾
console.log(stringValue.substring(3,7));//"lo w"//含头不含尾
console.log(stringValue.substr(3,7));//"lo worl"//第二个参数是要截取字符串的个数
console.log(stringVaule.slice(-3));//"rld"
console.log(stringValue.substring(-3));//"hello world"//substring()方法会将所有的负值参数都转换为0
console.log(stringValue.substr(-3));//"rld"
console.log(stringValue.slice(3,-4));//"lo w"//相当于slice(3,7)
console.log(stringValue.substring(3,-4));//"hel"//第二个参数转换成0,将较小的值作为开始的值
console.log(stringValue.substr(3,-4));//""(空字符串)//第二个参数转换成0

字符串位置方法

var stringValue="hello world";
console.log(stringValue.indexOf("o"));//4
console.log(stringValue.lastIndexOf("o"));//7
console.log(stringValue.indexOf("o",6));//7
console.log(stringValue.lastIndexOf("o",6));//4

trim()方法,字符串大小写转换

var stringValue="  hello world  ";
console.log(stringValue.trim());//"hello world"
console.log(stringValue.trimLeft());//"hello world  "
console.log(stringValue.trimRight());//"  hello world"
console.log(stringValue.toLocaleUpperCase());//"HELLO WORLD"
console.log(stringValue.toUpperCase());//"HELLO WORLD" 
console.log(stringValue.toLocaleLowerCase());//"hello world"
console.log(stringValue.toLowerCase());//"hello world"

在内置对象String中,有4个方法可以使用正则表达式,分别是search()、replace()、match()和split()
字符串模式匹配方法
match()会在字符串中检索匹配项,方法的参数和search相同,有结果返回一个组,没有结果就返回null。如果正则表达式中有标志"g",就返回包含所有匹配项的数组。

var text="cat,bat,sat,fat";
var pattern=/.at/;
var matches=text.match(pattern);
console.log(matches.index);//0
console.log(matches[0]);//"cat"
console.log(pattern.lastIndex)//0
var str="p12w34";
str.match(/\d+/g);//["12","34"]
//如果正则表达式没有标志"g",检索到第一个匹配项后就停止,返回和RegExp.exec()相同的结果,一个额外包含两个属性(index和input)的数组。index表示匹配项在字符串中索引,input表示被检索的字符串,数组中第一个元素是匹配的文本,接下里的元素都是分组中匹配到的文本。
//["12",index:1,input:"p12w34"]
str.match(/\d+/);
//["12","1","2",index:1,input:"p12w34"]
str.match(/(\d)(\d)/);

search()会返回第一个在字符串中与指定的正则表达式匹配的位置索引,匹配不到就返回-1.它只有一个参数:正则表达式,如果参数不是正则表达式,则会先自动转换成RegExp对象,再传入方法内。在匹配时,不支持全局检索,也就是会忽略标志字符串"g"

var text="cat,bat,sat,fat";
var pos=text.search(/at/);
console.log(pos);//1

replace()会先执行检索,匹配到指定的字符串后,再替换它。方法有两个桉树,第一个参数是需要替换的字符串或正则表达式,第二个参数是新的字符串。如果正则表达式设置了标志字符串"g",则执行全局模式。

var text="cat,bat,sat,fat";
var result=text.replace("at","ond");
console.log(result);//"cond,bat,sat,fat"

result=text.replace(/at/g,"ond");
console.log(result);//"cond,bond,sond,fond"
//在正则表达式中有分组的功能,replace()能够将字符串替换为捕获到的文本。使用由美元字符($)和数字组成的特殊变量作为替换占位符,数字代表组的索引序号,从1开始算起。
var str="p12w";
sre.replace(/(/d)(/d)/,"($1)($2)");//p(1)(2)w
result=text.replace(/(.at)/g,"word($1)");
console.log(result);//"word(cat),word(bat),word(sat),word(fat)"

split()有两个参数,第一个参数是把字符串拆分成一个数组的分隔符或正则表达式,第二个参数是可选的数值,用于限定分割项的数量,也就是数组的长度。

var colorText="red,blue,green,yellow";
console.log(colorText.split(","));//["red", "blue", "green", "yellow"]
console.log(colorText.split(",",2));//["red", "blue"]
console.log(colorText.split(/[^\,]+/));//["", ",", ",", ",", ""]

localeCompare()比较两个字符串在字母表中的位置:

  1. 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体情况的值要视现实而定);
  2. 如果字符串等于字符串参数,则返回0;
  3. 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数。
var stringValue="yellow";
console.log(stringValue.localeCompare("brick"));//1
console.log(stringValue.localeCompare("yellow"));//0
console.log(stringValue.localeCompare("zoo"));//-1

如何将字符串"get-element-by-id"转化为驼峰表示法的"getElementById"?
可以使用String对象中的replace()方法执行字符替换,replace的第二个参数可以定义为一个回调函数,该函数的参数:
match:匹配的文本
p1、p2…pn:分组中匹配的文本,即捕获的文本,p1表示第一个
index:匹配的文本在原字符串中的索引
input:被检索的字符串

var str="get-element-by-id";
str.replace(/-([a-z])/g,function(match,p1,index,input){
	return p1.toUpperCase();
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值