码迷,mamicode.com
首页 > Web开发 > 详细

JS引用类型(6)——基本包装类型2

时间:2016-09-26 12:46:58      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

(3)String类型

String类型是字符串包装类型,可以使用String构造函数来创建。

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

String对象的方法也可以在所有基本的字符串值中访问到。其中,继承的valueOf()、toLocalString()、toString()方法,都返回对象所表示的基本字符串值。
String类型的每个实例都有一个length属性,表示字符串中包含多个字符。

1》字符方法

  • charAt()方法:接受一个参数,基于0的字符位置。以单字符串的形式返回给定位置的那个字符。
    var stringValue = "hello world";
    alert(stringValue.charAt(1)); //"e"
  • charCodeAt()方法:接受一个参数,基于0的字符位置。返回给定位置的那个字符编码。
    var stringValue = "hello world";
    alert(stringValue.charAt(1)); //101
  • ECMAScript访问个别字符的方法。在支持该方法的浏览器(IE8+和其他)中,可以使用方括号加数字索引来访问字符串中的特定字符。在IE7及更早的版本中使用该语法,会返回undefined值。
    var stringValue = "hello world";
    alert(stringValue[1]); //"e"

2》字符串操作方法

  • concat()方法:用于将一或多个字符串拼接起来,返回拼接得到的新字符串。可以接受任意多个参数。【使用+操作符】
    var stringValue = "hello ";
    var result = stringValue.concat("world","!");
    alert(result); //"hello world!"
    alert(stringValue); //"hello"
  • slice()、substr()和substring()。这三个方法都会返回被操作字符串的一个子字符串,而且也都接受一或两个参数。第一个参数指定子字符串的开始位置,第二个参数表示子字符串到哪里结束。具体来说,slice()和substring()的第二个参数指定的是子字符串最后一个字符后面的位置。而substr()的第二个参数指定的则是返回的字符个数。如果没有给这些方法传递第二个参数,则将字符串的长度作为结束位置。与concat()方法一样,slice()、substr()和substring()也不会修改字符串本身的值,只是返回一个基本类型的字符串值。
    var stringValue = "hello world";
    alert(stringValue.slice(3)); //"lo world"
    alert(stringValue.substring(3)); //"lo world"
    alert(stringValue.substr(3)); //"lo world"
    alert(stringValue.slice(3,7)); //"lo w"
    alert(stringValue.substring(3,7)); //"lo w"
    alert(stringValue.substr(3,7)); //"lo worl"

  在传递给这些方法的参数是负值的情况下。slice()方法会将传入的负值与字符串的长度相加,substr()方法将负的第一参数加上字符串的长度,而将负的第二个参数转换为0.最后,substring()方法会把所有负值参数都转换为0.

var stringValue = "hello world";
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"lrld"
alert(stringValue.slice(3,-4)); //"lo w"
alert(stringValue.substring(3,-4)); //"hel"
alert(stringValue.substr(3,-4)); //" "

  IE的JavaScript实现在处理向substr()传递负值的情况时存在问题,它会返回原始的字符串。IE9修复了这个问题。

3》字符串位置方法

  • indexOf()方法:从一个字符串的开头向后搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1。
  • lastIndexOf()方法:从一个字符串的末尾向前搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1。
var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7

这两个方法都可以接收可选的第二个参数,表示从字符串中的哪个位置开始搜索。

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

在使用第二个参数的情况下,可以通过循环调用indexOf()或lastIndexOf()方法来找到所有匹配的子字符串。

var stringValue = "Lorem ipsum dolor sit amet,consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");

while(pos>-1){
positions.push(pos);
pos = stringValue.indexOf("e",pos+1);
}

alert(positions); //"3,24,32,35,52"

4》trim()方法

trim()方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。

var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue); //"hello world"

支持这个方法的浏览器有IE9+和其他。此外,除IE9+以外,其他浏览器还支持非标准的trimLeft()和trimRight()方法,分别用于删除字符串开头和末尾的空格。

5》字符串大小写转换方法

toLowerCase()、toLocalLowerCase()、toUpperCase()、toLocalUpperCase()

6》字符串的模式匹配方法

  • match()方法:在字符串上调用这个方法,本质上与调用RegExp的exec()方法相同。match()方法只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。返回一个数组。
    var text = "cat,bat,sat,fat";
    var pattern = /.at/;
    
    //与pattern.exec(text)相同
    var matches = text.match(pattern);
    alert(matches.index); //0
    alert(matches[0]); //"cat"
    alert(matches.lastIndex); //0
  • search()方法:这个方法唯一的参数与match()方法的参数相同,由字符串或RegExp对象指定的一个正则表达式。返回字符串中第一个匹配项的索引。如果没有找到匹配项,则返回-1。此方法始终是从字符串开头向后查找模式。
    var text = "cat,bat,sat,fat";
    var pos = text.search(/at/);
    alert(pos); //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"

  如果第二个参数是字符串,可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中。
  replace()方法的第二个参数也可以是一个函数。在只有一个匹配项的情况下,会向这个函数传递3个参数:模式的匹配项、模式匹配项在字符串中的位置和原始字符串。

function htmlEscape(text){
return text.replace(/[<>"&]/g,function(match,pos,originalText){
switch(match){
case "<":
return "&lt;";
case ">":
return "&gt;";
case "&":
return "&amp;";
case "\"":
return "&quot;";
}
});
}
alert(htmlEscape("<p class = \"greeting\">Hello world!</p>"));
//&lt;p class=&quot;greeting&quot;&gt;Hello world!&lt;/p&gt;
  • split()方法:基于指定的分隔符将一个字符串分割成多个子字符串,并将结果放在一个数组中。此方法接受可选的第二个参数,用于指定数组的大小,以便确保返回的数组不会超过既定大小。
    var colorText = "red,blue,green,yellow";
    var colors1 = colorText.split(","); //["red","blue","green","yellow"]
    var colors2 = colorText.split(",",2); //["red","blue"]
    var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]

7》localeCompare()方法

localeCompare()方法比较两个字符串,并返回下列值中的一个:

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

8》fromCharCode()方法

接受一个或多个字符编码,然后将它们转换成一个字符串。

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

 

JS引用类型(6)——基本包装类型2

标签:

原文地址:http://www.cnblogs.com/LS-tuotuo/p/5908509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!