(3)String类型
String类型是字符串包装类型,可以使用String构造函数来创建。
var stringObject = new string("hello world");
String对象的方法也可以在所有基本的字符串值中访问到。其中,继承的valueOf()、toLocalString()、toString()方法,都返回对象所表示的基本字符串值。
String类型的每个实例都有一个length属性,表示字符串中包含多个字符。
var stringValue = "hello world"; alert(stringValue.charAt(1)); //"e"
var stringValue = "hello world"; alert(stringValue.charAt(1)); //101
var stringValue = "hello world"; alert(stringValue[1]); //"e"
var stringValue = "hello "; var result = stringValue.concat("world","!"); alert(result); //"hello world!" alert(stringValue); //"hello"
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修复了这个问题。
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"
trim()方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。
var stringValue = " hello world "; var trimmedStringValue = stringValue.trim(); alert(stringValue); //" hello world " alert(trimmedStringValue); //"hello world"
支持这个方法的浏览器有IE9+和其他。此外,除IE9+以外,其他浏览器还支持非标准的trimLeft()和trimRight()方法,分别用于删除字符串开头和末尾的空格。
toLowerCase()、toLocalLowerCase()、toUpperCase()、toLocalUpperCase()
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
var text = "cat,bat,sat,fat"; var pos = text.search(/at/); alert(pos); //1
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 "<"; case ">": return ">"; case "&": return "&"; case "\"": return """; } }); } alert(htmlEscape("<p class = \"greeting\">Hello world!</p>")); //<p class="greeting">Hello world!</p>
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(/[^\,]+/); //["", ",", ",", ",", ""]
localeCompare()方法比较两个字符串,并返回下列值中的一个:
接受一个或多个字符编码,然后将它们转换成一个字符串。
alert(String.fromCharCode(104,101,108,111)); //"hello"
原文:http://www.cnblogs.com/LS-tuotuo/p/5908509.html