1 <script type="text/javascript">
2 //1、Boolean 类型
3 //2、Number 类型
4 //3、String 类型
5
6 //Boolean类型容易与基本类型混淆,所以建议永远不要使用Boolean对象。
7
8 //Number是与数字对应的引用类型
9 var numberObj = new Number(10);
10 //重写toString方法 传入的参数是告诉它放回几进制数字的字符串类型
11 var num = 10;
12 alert(num.toString());//"10"
13 alert(num.toString(2));//"1010"
14 alert(num.toString(8));//"12"
15 alert(num.toString(10));//"10"
16 alert(num.toString(16));//"a"
17
18 //toFixed()方法,是返回指定小数位的数值的字符串表示方法,而且具有四舍五入的功能
19 var num = 10;
20 num.toFixed(2);//"10.00"
21
22
23 //toExponential()指数表示法方法,接受一个参数表示输出结果中小数的位数
24 var num = 10;
25 alert(num.toExponential(1));//"1.0e+1"
26 //不过这么小的数字就不需要使用指数表示法了,如果你想得到某个数值最合适的格式就应该使用
27 //toPrecision()方法,此方法可能返回固定大小(fixed)格式,也可能返回指数(exponential)格式
28 //接受一个参数表示数值所有数字的位数(不包括指数部分)。
29 var num = 99;
30 alert(num.toPrecision(1));//1e+2,1e+2表示100,因为指数无法表示99所以向上舍入变成100
31 alert(num.toPrecision(2));//"99"
32 alert(num.toPrecision(3));//"99.0"
33
34
35 //String对象,String对象的方法也可以在所有的基本字符串中访问到。
36 //1、字符操作方法:charAt()、charCodeAt()。每个参数都接受一个基于位置0的字符位置
37 var stringValue = "Hello world!";
38 stringValue.charAt(1);//"e" 第二个位置是“e”
39 stringValue.charCodeAt(1);//"101" 第二个位置“e”的字符编码是“101”
40
41
42 //2、字符串操作方法concat(拼接的字符)、slice(index,index)、substring(index,index)、substr(index,length)。index:位置,length:长度
43 var str1 = "hello";
44 alert(str1.concat(" word"));//Hello world
45 alert(str1.concat(" word", "!"));//Hello world!
46
47 var stringValue = "Hello world!";
48 alert(stringValue.slice(3));//lo world
49 alert(stringValue.substring(3));//lo world
50 alert(stringValue.substr(3));//lo world
51
52 alert(stringValue.slice(3, 7));//lo w
53 alert(stringValue.substring(3, 7));//lo w
54 alert(stringValue.substr(3, 7));//lo worl 这个7代表截取的长度
55
56 //3、字符串位置方法 indexOf() 和 lastIndexOf()
57 //这两个方法都是从指定的字符串中搜索给定的字符串,然后返回字符串的位置,没有找到就返回-1。
58 //这两个方法的区别在于一个是从字符串的开头向后搜索字符串,而lastIndexOf是从字符串的末尾向前搜索字符串。
59 //这两个方法都有一个可选的参数(从指定的位置开始搜索)
60 var stringValue = "hello word";
61 alert(stringValue.indexOf("o"));//4
62 alert(stringValue.lastIndexOf("o"));//7
63 //可以循环调用indexOf或lastIndexOf来找到指定的字符串
64
65 var stringValue = "wo de wei lai bu shi meng!wo men you geng hao de ming tian!";
66 var positions = [];
67 var pos = stringValue.indexOf("e");
68
69 while (pos > -1) {
70 positions.push(pos);
71 pos = stringValue.indexOf("e", pos + 1);
72 }
73 alert(positions);//4、7、22、33、38、47
74
75
76 //4、trim()这个方法会创建一个字符串副本,删除前置及后置的所有空格。
77 var stringValue=" hello word ";
78 alert(stringValue);
79 alert(stringValue.trim());
80
81
82 //5、字符串大小写转换方法
83 //toLowerCase、toLocalLowerCase、toUpperCase、toLocalUpperCase
84 var stringValue="hello word";
85 alert(stringValue.toLocaleUpperCase());//此方法比较稳妥
86 alert(stringValue.toUpperCase());
87 alert(stringValue.toLocaleLowerCase());//此方法比较稳妥
88 alert(stringValue.toLowerCase());
89
90 //6、字符串匹配方法 replace()
91 //这个方法接受两个参数,第一个参数是一个正则表达式或者字符串,第二个参数是一个字符串或一个函数
92 var text="cat,bat,sat,fat";
93 var result=text.replace("at","ond");//
94 alert(result);//"cond,bond,sond,fond"
95
96 var result=text.replace(/at/g,"ond");//
97 alert(result);//"cond,bond,sond,fond"
98
99
100 var text="cat,bat,sat,fat";
101 result=text.replace(/(.at)/g,"word ($1)");
102 alert(result);
103
104
105 //replace的第二个参数也可以是一个函数
106 function htmlEscape(text) {
107 //函数有是三个参数:1、模式匹配项 2、模式匹配项在字符中的位置 3、原始字符串
108 return text.replace(/[<>"&]/g,function(match,index,text){
109 switch (match){
110 case "<":
111 return "<";
112 case ">":
113 return ">";
114 case "&":
115 return "&";
116 case "\"":
117 return """;
118 }
119 });
120 }
121
122
123 alert(htmlEscape("<p class=\"greeting\">Hello World!</p>"));
124 //<p class="greeting">Hello World!</p>
125 //localCompare()比较两个字符串。A.localCompare("B")
126 //如果字符串(A)在字母表中排在字符串参数(B)之前,这返回负数(-1)
127 //如果字符串等于字符串参数则返回0
128 //如果字符串(A)在字母表中排在字符串参数(B)之后则返回正数(1)
129
130 var stringValue="f";
131 alert(stringValue.localeCompare("d"));//1
132 alert(stringValue.localeCompare("f"));//0
133 alert(stringValue.localeCompare("z"));//-1
134
135 //fromCharCode 这个静态方法是与charCodeAt执行相反的操作
136 alert(String.fromCharCode(104,101,108,108,111));//"hello"
137
138
139 //7、html方法建议不要使用。
140 </script>