5.6 基本包装类型
5.6.1 Boolean类型(略)
5.6.2 Number类型(略)
5.6.3 String类型
1)、创建方法
var strObj = new String("我的名字");
2)、字符方法
document.write(strObj.length); // 11 显示字符串长度
document.write(strObj.charAt(1)); // e 以0为基数,获取指定位置的字符
document.write(strObj.charCodeAt(1)); // e 以0为基数,获取指定位置的字符的编码
document.write(strObj[1]); // e 得到指定序号的字符
3)、字符串的操作方法
a)、拼接concat()
var strObj = new String("hello ");
var newStr = strObj.concat("world");
alert(newStr); // hello world 不会影响strObj
b)、截取
var str = "hello world";
alert(str.slice(3)); // lo world 取第3个到最后位置
alert(str.substring(3)); // lo world 取第3个到最后位置
alert(str.substr(3)); // lo world 取第3个,长度没有则为最大长度
alert(str.slice(3,7)); // lo w 取第3个到第6个
alert(str.substring(3,7)); // lo w 取第3个到第6个
alert(str.substr(3,7)); // lo worl 取第3个开始,长度为7
如果指定的序号是负数:
alert(str.slice(-3)) 等同 alert(str.slice(str.length - 3));
alert(str.slice(3,-4)) 等同 alert(str.slice(3,str.length - 4));
alert(str.substring(-3)); // hello world 负数变为0
alert(str.substring(3,-4)); // 0,1,2 hel
alert(str.substr(-3)); // rld 总长-3=8,只有一个参数8时,会截取总长度
alert(str.substr(3,-4)); // 第二个参数为负数的话,则变为0,结果为空
c)、字符串位置方法
var str = "hello world";
alert(str.indexOf("e")); // 1 从前往后查找指定的字符首次出现的位置
alert(str.indexOf("e",3)); // -1 从前住后,从第3个符号开始查找指定的字符串位置,如果未找到则为-1
alert(str.lastIndexOf("e")); // 1 从后往前查找指定的字符首次出现的位置
alert(str.lastIndexOf("e",3)); // 1 从后住前,从第3个符号开始查找指定的字符串位置,如果未找到则为-1
要注意第二条与第四条代码,它们的结果是有差别的喔,第二条结果为-1,那是因为从序号3开始往后没有字符e了,所以为-1;
但如果从后往前的话,从序号3开始,序号1正是我们要找的字符e,所以为1。
个人觉得有必要将查询字符串位置列表的功能做成一个函数:
function searchPostion(str,e){
var position = new Array();
var pos = str.indexOf(e)
while (pos != -1){
position.push(pos);
pos = str.indexOf(e,pos + 1);
}
return position;
}
var mystr = "Lorem ipsum dolor site amet,consectetur adipisicing elit";
result = searchPostion(mystr,"e"); // 调用函数,查找字符e的位置
alert(result); // 3,21,25,32,35,52
d)、trim删除前置与后置空格,生成新的字符串
var str = " hello world ";
var newStr = str.trim();
alert(newStr); // hello world
注:即使str前后都没有空格,使用trim后也会生成新的字符串newStr。
e)、转换为大小写:
var str = "Hello World";
var 大写 = str.toUpperCase();
var 小写 = str.toLowerCase();
alert(大写); // HELLO WORLD
alert(小写); // hello world
f)、在字符串中应用正则表达式
1)、match方法
var str = "cat,bat,sat,fat"
var pat = /.at/;
var res = str.match(pat);
alert(res.index);
alert(res[0]);
alert(pat.lastIndex);
2)、search方法,此方法返回的是等查字符首次出现的字符位置
3)、替换字符串
var str = "cat,bat,sat,fat"
var res1 = str.replace("at","ond");
alert(res1); // cond,bat,sat,fat,只替换找到的第一个
var res2 = str.replace(/at/g,"ond");
alert(res2); // cond,bond,sond,fond,由于加了全局标记g,替换全部
注:模式两边不能有双引号
4)、replace第2个参数为函数
function htmlEscape(str){
return str.replace(/[<>"&]/g,function (match,pos,originalText) {
switch (match) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "\"":
return """;
}
});
}
var res = htmlEscape("<p class=\"greeting\">Hello world!</p>");
alert(res);
5)、split
var str = "red,blue,green,yellow";
var colors = str.split(","); // 以逗号作为分隔
alert(colors); // red,blue,green,yellow
alert(typeof(colors)); // 显示类型为object
var colors2 = str.split(",",2); // 以逗号作为分隔,指定数组大小为2个元素
alert(colors2); // red,blue
注:第一个参数作为分隔符,还可以使用正则表达式来分隔更复杂的情况,如下:
var str = "red blue,green|yellow";
var colors = str.split(/[ ,|]/g); // 以空格,逗号和|作为分隔
alert(colors); // red,blue,green,yellow
6)、localeCompare
var str = "y";
alert(str.localeCompare("b")); // 排在给出的字符串之后,即y在b之后,则为1
alert(str.localeCompare("y")); // 排在给出的字符串相同,则为0
alert(str.localeCompare("z")); // 排在给出的字符串之前,即y在z之前,则为-1
7)、fromCharCode给出字符编码转为字符
// String静态方法,即没有实例
var str = String.fromCharCode(104,101,108,108,111);
alert(str); // hello
本文暂时没有评论,来添加一个吧(●'◡'●)