js学习笔记:基本包装类型——String

String类型是字符串的对象包装类型,可以像下面这样使用String构造函数来创建:

var stringObject = new String("hello world");

String继承的valueOf,toString和toLocaleString都返回对象所表示的基本字符串值。
String的每个实例都有一个length属性,表示字符串中包含多少字符。

字符方法

charAt() charCodeAt()

  • 接收一个参数,即基于0的字符位置。
  • charAt以单字符字符串的形式返回给定位置的那个字符。
  • charCodeAt则返回该位置字符的字符编码
var stringValue = "hello world";
stringValue.charAt(1); //"e"
stringValue.charCodeAt(1); //"101"

另外,也可以使用方括号加数字索引来访问字符串中的特定字符。

var stringValue = "hello world";
stringValue[1]; //"e"

字符串操作方法

concat()

用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。

var stringValue = "hello ";
var result  = stringValue.concat("world");
alert(result); //"hello world"
alert(stringValue); //"hello"

concat方法可以接受任意多个参数,也就是说可以通过它拼接任意多个字符串。

var stringValue = "hello ";
var result  = stringValue.concat("world","!");
alert(result); //"hello world!"
alert(stringValue); //"hello"

虽然concat是专门用来拼接字符串的方法,但实践中使用更多的还是加号操作符,比concat更简单易行。

slice() substr() substring()

  • 这三个方法都会返回被操作字符串的一个子字符串
  • 都接受一或两个参数
    • 第一个参数指定子字符串的开始位置
    • 第二个参数表示子字符串到哪里结束
      • slice()和substring()的第二个参数指定的是子字符串最后一个字符后面的位置。
      • substr()的第二个参数指定的是返回的字符个数
    • 若没有第二个参数,则将字符串的长度作为结束位置
  • 与concat方法一样,也不会修改字符串本身的值
var stringValue = "hello world";
stringValue.slice(3); //"lo world"
stringValue.substring(3); //"lo world"
stringValue.substr(3); //"lo world"

stringValue.slice(3,7);  //"lo w"
stringValue.substring(3,7); //"lo w"
stringValue.substr(3,7);  //"lo worl"

当传递负值时,这三个方法的表现就不太一样了。

  • slice会将传入的负值与字符串长度相加
  • substr将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0
  • substring会把所有的负数转换为0

substring和splice基本相同,但是无法处理负数,因此最好使用slice()方法替代它。

var stringValue = "hello world";
stringValue.slice(-3); //"rld"
stringValue.substring(-3); //"hello world"
stringValue.substr(-3); //"rld"

stringValue.slice(3,-4);  //"lo w"
stringValue.substring(3,-4); //"hel"
stringValue.substr(3,-4);  //""

对于substring(3,-4),由于substring会把第二个参数变为0,因此变成substring(3,0),由于这个方法会将较小的数作为开始位置,因此就相当于调用了substring(0,3),得到“hel”。

substr会将第二个参数变为0,这就意味着返回包含零个字符的字符串,也就是空字符串。

字符串位置方法

indexOf() lastIndexOf()

  • 都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置(如果没有找到子字符串,则返回-1)
  • indexOf从字符串开头向后搜索,lastIndexOf则从字符串的末尾向前搜索
var stringValue = "hello world";
stringValue.indexOf("o");  //4
stringValue.lastIndexOf("o"); //7
  • 都接受可选的第二个参数,表示从字符串中的哪个位置开始搜索。
var stringValue = "hello world";
stringValue.indexOf("o",6);  //7
stringValue.lastIndexOf("o",6); //4

在使用第二个参数的情况下,可以循环调用indexOf或lastIndexOf来找到所有匹配的字符串。(如:每次匹配后都将第二个参数设为当前找到的位置+1,继续向后搜索)

trim()

创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。
chrome 8+还支持非标准的trimLeft与trimRight方法,分别用于删除字符串开头和末尾的空格。

字符串大小写转换方法

均返回一个新字符串。

toLowerCase()

toLocaleLowerCase()

toUpperCase()

toLocaleUpperCase()

字符串的模式匹配方法

match()

在字符串上调用这个方法,本质上与调用RegExp的exec()方法相同。
只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。

var text = "cat, bat, sat fat";
var pattern = /.at/;
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0

match方法返回了一个数组。

  • 这个方法的唯一参数与match方法的参数相同:RegExp对象或一个正则表达式。
  • 返回字符串中第一个匹配项的索引;如果没找到则返回-1.
  • 始终是从字符串开头向后查找
var text = "cat, bat, sat fat";
var pos = text.search(/at/);
alert(pos);//1

这个例子中的search方法返回了“at”在字符中第一次出现的位置,1.

replace()

  • 为了简化替换子字符串的操作
  • 接收两个参数
    • 第一个参数可以是一个RegExp对象或者一个字符串
    • 第二个参数可以是一个字符串或者一个函数

如果第一个参数是字符串,那么只会替换第一个子字符串。要想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要制定全局g标志。

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

result = text.replace(/at/g,"ond");
alert(result); //cond, bond, sond, fond

split()

  • 可以基于指定的分隔符将一个字符串分割成多个子字符串,并将结果放在一个数组中。
  • 分隔符可以是字符串,也可以是一个RegExp对象。
  • 可以接受可选的第二个参数,用于指定数组的大小,以便确保返回的数组不会超过既定大小。
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");//["red","blue","green","yellow"]
var colors2 = colorText.split(",",2);//["red","blue"]

localeCompare()

这个方法比较两个字符串,并返回下列值中的一个

  • 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数
  • 如果字符串等于字符串参数,返回0
  • 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数
var stringValue = "yellow";
stringValue .localeCompare("brick"); //1
stringValue .localeCompare("yellow"); //0
stringValue .localeCompare("zoo"); //-1

因为localeCompare返回的数值取决于实现,所以最好可以封装一下此方法根据自己的需求返回不同的结果。

fromCharCode()

  • 是String构造函数的静态方法
  • 接收一个或多个字符编码,然后将它们转换成一个字符串
  • 从本质上看,与charCodeAt执行的是相反的操作。
String.fromCharCode(104,101,108,108,111);  //"hello"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值