String类型

String类型

一、相关概念

1.定义

  1. String类型是字符串的对象包装类型
  2. 创建String对象
var stringObject=new String("hello world");

2.补充

  1. String对象大方法也可以在所有基本的字符串值中访问到
  2. 继承的valueOf()toLocaleString()toString()方法,都返回对象所表示的基本字符串值

二、属性

1.length

  1. String类型的每个实例都有一个length属性,表示字符串中包含多个字符
  2. 字符串中包含双字节字符(不是占一个字节的ASCII字符),每个字符仍然算一个字符
var stringValue=new String("hello world");
alert(stringValue.length);/"11"

三、方法

(一).字符方法

1.charAt()
  1. 接收一个参数,基于0的字符位置
  2. 以单字符字符串的形式返回给定位置的哪个字符(ECMAScript中没有字符类型)
var stringValue=new String("hello world");
alert(stringValue.charAt(1));//"e"
2.charCodeAt()
  1. 接收一个参数,基于0的字符位置
  2. 以单字符字符串的形式返回给定位置的哪个字符的字符编码
var stringValue=new String("hello world");
alert(stringValue.charCodeAt(1));//"101"
3.方括号表示法
  1. 在支持的浏览器中,可以使用方括号加数字索引来访问字符串中的特定字符
  2. 使用方括号表示法访问个别字符的语法得到了IE8FirefoxSafariChromeOpera所有版本的支持
  3. 如果是在IE7及更早版本中使用这种语法,会返回undefined值(尽管根本不是特殊的undefined值)
var stringValue=new String("hello world");
alert(stringValue[1]);//"e"

(二).字符串操作方法

1.concat()
  1. concat()用于将一或多个字符串拼接起来,返回拼接得到的新字符串。不改变原字符串
  2. concat()方法可以接收任意多个参数,也就是说可以通过它拼接任意多个字符串
  3. 虽然concat()是专门用来拼接字符串的方法,但实践中使用更多的还是加号操作符(+)。而且,使用加号操作符在大多数情况下都比使用concat()方法要简便易行(特别是在拼接多个字符串的情况下)
var stringValue=new String("hello ");
var result=stringValue.concat("world");
alert(result);//"hello world"
alert(stringValue);//"hello "
var stringValue=new String("hello ");
var result=stringValue.concat("world","!");
alert(result);//"hello world!"
alert(stringValue);//"hello "
2.slice()
  1. 返回被操作数的一个子字符串,而且也接收一或两个参数。第一个参数指定子字符串的开始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束
  2. 第二个参数指定的是子字符串最后一个字符后面的位置
  3. 如果没有给这些方法传递第二个参数,则将字符串的末尾作为结束吧位置
  4. 不会修改字符串本身的值——只是返回一个基本类型的字符串值,对原字符串没有任何影响
  5. 传入参数是负数时,slice()会将传入的负值与字符串的长度相加
var stringValue=new String("hello world");
alert(stringValue.slice(3));//"lo  world" 
alert(stringValue.slice(3,7));//"lo w" 
var stringValue=new String("hello world");
alert(stringValue.slice(-3));//"rld"
alert(stringValue.slice(3,-4));//"lo w"
3.substr()
  1. 返回被操作数的一个子字符串,而且也接收一或两个参数。第一个参数指定子字符串的开始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束
  2. 第二个参数指定的是返回的字符个数
  3. 如果没有给这些方法传递第二个参数,则将字符串的末尾作为结束吧位置
  4. 不会修改字符串本身的值——只是返回一个基本类型的字符串值,对原字符串没有任何影响
  5. 传入参数是负数时,substr()方法会将负的第一个参数加上字符串的长度,而将将负的第二个参数转换为0
var stringValue=new String("hello world");
alert(stringValue.substr(3));//"lo  world"
alert(stringValue.substr(3,7));//"lo worl"
var stringValue=new String("hello world");
alert(stringValue.substr(-3));//"rld"
alert(stringValue.substr(3,-4));//""
//substr()会将第二个参数转换为0,返回的字符串包含零个字符串,也就是一个空字符串
4.substring()
  1. 返回被操作数的一个子字符串,而且也接收一或两个参数。第一个参数指定子字符串的开始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束
  2. 第二个参数指定的是子字符串最后一个字符后面的位置
  3. 如果没有给这些方法传递第二个参数,则将字符串的末尾作为结束吧位置
  4. 不会修改字符串本身的值——只是返回一个基本类型的字符串值,对原字符串没有任何影响
  5. 传入参数是负数时,substring()方法会将所有的负值参数都转换为0
var stringValue=new String("hello world");
alert(stringValue.substring(3));//"lo  world"
alert(stringValue.substring(3,7));//"lo w"
var stringValue=new String("hello world");
alert(stringValue.substring(-3));//"hello  world"
alert(stringValue.substring(3,-4));//"hel"
//substring()方法会把第二个参数转换为0,也就是substring(3,0),而由于这个方法会将较小的数作为开始位置,将较大数作为结束位置,最终相当于调用了substring(0,3)

(三).字符串位置方法

1.indexOf()
  1. 从一个字符串中搜索给定的字符串,然后返回怒字符串的位置(如果没有找到该字符串,则返回-1)
  2. indexOf()方法从字符串的开头向后搜索字符串
  3. 如果给定字符串仅出现一次,那么indexOf()lastIndexOf()会返回相同的位置值
  4. 第二个参数(可选)表示从字符串中的哪个位置开始搜索
  5. indexOf()会从该参数指定的位置向后搜索,忽略该位置之前的所有字符
var stringValue=new String("hello world");
alert(stringValue.indexOf("o"));//4
var stringValue=new String("hello world");
alert(stringValue.indexOf("o",6));//7
var strinngValue="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);
2.lastIndexOf()
  1. 从一个字符串中搜索给定的字符串,然后返回怒字符串的位置(如果没有找到该字符串,则返回-1)
  2. lastIndexOf()方法是从字符串的末尾向前搜索字符串
  3. 第二个参数(可选)表示从字符串中的哪个位置开始搜索
  4. lastIndexOf()会从该参数指定的位置向前搜索,忽略该位置之后的所有字符
var stringValue=new String("hello world");
alert(stringValue.lastIndexOf("o"));//7
var stringValue=new String("hello world");
alert(stringValue.lastIndexOf("o",6));//4

(四).trim()方法

  1. trim()会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果
  2. trim()返回的是字符串的副本,所以原始字符串中的前置及后缀空格会保持不变
  3. 支持这个方法的浏览器有IE 9+Firefox 3.5+Safari 5+Opera 10.5+Chrome
  4. 此外,Firefox 3.5+Safari 5+Chrome 8+还支持非标准的trimLeft()trimRight()方法,分别用于删除字符串开头和末尾的空格
var stringValue=new String(" hello world ");
var trimmedStringValue=stringValue.trim();
alert(stringValue);" hello world "
alert(trimmedStringValue);//"hello world"

(五).字符串大小写转换方法

1.toLowerCase()
  1. 借鉴自java.lang.String中的同名方法,将对象转换为小写
var stringValue=new String("hello world");
alert(stringValue.toLowerCase());//"hello world"
2.toLocaleLowerCase()
  1. 针对特定时区,将对象转换为小写
  2. 一般来说,在不知道自己的代码将在哪种语言环境中运行的情况下,还是使用针对地区的方法更稳妥一些
var stringValue=new String("hello world");
alert(stringValue.toLocaleLowerCase());//"hello world"
3.toUpperCase()
  1. 借鉴自java.lang.String中的同名方法,将对象转换为大写
var stringValue=new String("hello world");
alert(stringValue.toUpperCase());//"HELLO WORLD"
4.toLocaleUpperCase()
  1. 针对特定时区,将对象转换为大写
  2. 一般来说,在不知道自己的代码将在哪种语言环境中运行的情况下,还是使用针对地区的方法更稳妥一些
var stringValue=new String("hello world");
alert(stringValue.toLocaleUpperCase());//"HELLO WORLD"

(六).字符串的模式匹配方法

1.match()
  1. match(),在字符串上调用这个方法,本质上与调用RegExp()exec()方法相同
  2. match()方法只接收一个参数,要么是一个正则表达式,要么是一个RegExp对象
var text=new String("cat,bat,sat,fat");
var pattern=/.at/;
//与pattern.exec(text)相同
var matches=text.match(pattern);
alert(matches.index);//0
alert(matches[0]);
alert(pattern.lastIndex);//0
//match()方法返回了一个数组
//如果是调用RegExp对象的exec()方法并传递本例中的字符串作为参数,那么也会得到与此相同的数组:数组的第一项是与整个模式匹配的字符串,之后的每一项(如果有)保存着与正则表达式中的捕获组匹配的字符串
2.search()
  1. search()方法的唯一参数与match()方法的参数相同:由字符串或RegExp对象指定的一个正则表达式
  2. search()方法返回字符串中第一个匹配项的索引;如果没有找到匹配项,则返回-1
  3. search()方法始终是从字符串开头向后查找模式
var text=new String("cat,bat,sat,fat");
var pos=text.search(/at/);
alert(pos);//1
//search()方法返回1,即"at"在字符串中第一次出现的位置
3.replace()
  1. replace()用于替换字符串
  2. 这个方法接收两个参数:第一个参数可以是一个RegExp对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参数可以是一个字符串或者一个函数
  3. 如果第一个参数是字符串,那么只会替换第一个字符串。要想替换所有子字符串,唯一的办法就是提供一个正则表达式,而且要指定全局(g)标志
  4. 如果第二个参数是字符串,那么还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中
  5. replace()方法的第二个参数也可以是一个函数。在只有一个匹配项(即与模式匹配的字符串)的情况下,会向这个函数传递3个阐述:模式匹配、模式匹配项在字符串中的位置和原始字符串。在正则表达式中定义了多个捕获组的情况下,传递给函数的参数依次是模式的匹配项、第一个捕获组的匹配项、第二个捕获组的匹配项······,但最后两个参数仍然分别是模式的匹配项在字符串中的位置和原始字符串。这个函数应该返回一个字符串,表示应该被替换的匹配项。使用函数作为replace()方法的第二个参数可以实现更加精细的替换操作
function htmlEscape(text){
	return text.replace(/[<>"&]/g,function(match,pos,originalText){
		switch(match){
			case "<":return "<";
			case ">":return "↦";
			case "\"":return """;
		}
	};)
}
alert(htmlEscape("

Hello world!

")); //<p class="greeting">Hello world!</p>
字符序列 替换文本
$$ $
$& 匹配整个模式的子字符串。与RegExp.lastMatch的值相同
$' 匹配的子字符串之前的字符串。与RegExp.leftContext的值相同
$` 匹配的子字符串之后的子字符串。与RegExp.rightContext的值相同
$n 匹配第n个捕获组的子字符串,其中n等于0~9.例如,$1是匹配第一个捕获组的子字符串,$2是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串
$nn 匹配第nn个捕获组的子字符串,其中nn等于01~99。例如,$01是匹配第一个捕获组的子字符串,$02是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串
var text=new String("cat,bat,sat,fat");
var result=text.replace("at","ond");
alert(result);//"cond,bat,sat,fat"
//传入replace()方法的是字符串"at"和替换用的字符串"ond"。替换的结果是把"cat"变成了"cond",但字符串中的其他字符并没有收到任何影响

result=text.replace(/at/g,"ond");
alert(result);//"cond,bond,sond,fond"
//通过将第一个参数修改为带有全局标志的正则表达式,就将全部"at"都替换成了"ond"
var text=new String("cat,bat,sat,fat");
result=text.replace(/(.at)/g,"word($1)");
alert(result);//word(cat),word(bat),word(sat),word(fat)
//在此,每个以"at"结尾的单词都被替换了,替换结果是"word"后跟一对圆括号,而圆括号中是被字符序列$1所替换的单词
4.split()
  1. split()方法可以基于指定的分隔符将一个字符串分隔成多个字符串,并将结果放在一个数组中。分隔符可以是字符串,也可以是一个RegExp对象(这个方法不会将字符串看成正则表达式)
  2. split()方法可以接受可选的第二个参数,用于指定数组的大小,以便确保返回的数组不会超过既定大小
  3. split()正正则表达式的支持因浏览器而异。尽管对于简单的模式没什么差别,但对于未发现匹配项及带有捕获组的模式,匹配的行为大不相同
    • IE8及之前版本会忽略捕获组。ECMA-262规定应该把捕获组拼接到结果数组中。IE9能正确地在结果中包含捕获组
    • Firefox 3.6及之前版本在捕获组未找到匹配项时,会在结果数组中包含空字符串;ECMA-262规定没有匹配项的捕获组在结果数组中应该用undefined表示
var colorText=new String("red,blue,green,yellow");
var colors1=colorText.split(",");//["red","blue","green","yellow"]
var colors1=colorText.split(",",2);//["red","blue"]
var colors1=colorText.split(/[^\,]+/);//["",",",",",""]
//通过正则表达式指定的分隔符出现在了字符串的的开头(即子字符串"red")和末尾(即子字符串"yellow")

(七).localeCompare()方法

  1. localeCompare()方法比较两个字符串,并返回下列值中的第一个
    • 如果字符串在字母表中应该在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值要视实现而定)
    • 如果字符串等于字符串参数,则返回0
    • 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数(大多数情况下是-1,具体的值同样要视实现而定)
var stringValue=new String("yellow");
alert(stringValue.localeCompare("brick"));//1
alert(stringValue.localeCompare("yellow"));//0
alert(stringValue.localeCompare("zoo"));//-1
function determineOrder(value){
	var result=stringValue.localeCompare(value);
	if(result<0){
		alert("The string 'yellow' comes before the string '"+value+"'.");
	}else if(result>0){
		alert("The string 'yellow' comes after the string '"+value+"'.");
	}else{
		alert("The string 'yellow' is equal to the string '"+value+"'.");
	}
}
determineOrder("brick");
determineOrder("yellow");
determineOrder("zoo");

(八).fromCharCode()方法

  1. String构造函数本身还有一个静态方法:fromCharCode()。这个方法的任务是接受一或多个字符编码,然后将它们转换成一个字符串
  2. 从本质上看,这个方法与实例方法charCodeAt()执行的是相反的操作
alert(String.fromCharCode(104,101,,108,108,111));//"hello"
//给fromCharCode()传递的是字符串"hello"中每个字母的字符编码

(九).HTML方法

  1. 早期的Web浏览器提供商觉察到了使用JavaScript动态格式化HTML的需求。这些提供商就扩展了标准,实现了一些专门用于简化常见HTML格式化任务的方法
  2. 应该尽量不使用这些方法,因为它们创建的标记通常无法表达语义
方法 输出结果
anchor(name) string
big() string
bold() string
fixed() string
fontcolor() string
fontsize() string
italics() string
link(url) string
small() string
strike() string
sub() string
sup() string

源自
源于整理《JavaScript高级程序设计》

你可能感兴趣的:(js)