1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <title>字符串操作</title>
6 <script type="text/javascript">
7 window.onload = function(){
8 var str1 = '人生若只如初见,何事秋风悲画扇。';
9 var str2 = 'hanyuluoxuan184';
10 var str3 = '0123456789';
11
12 // console.log(str1.length); //16
13 // console.log(str1.charAt(1)); //生
14 // console.log(str1.charCodeAt(1)); //人-->20154 ,生 -->29983
15 // console.log(String.fromCharCode(20154,29983));//人生
16 // console.log(str1.indexOf('见'));//6
17 // console.log(str1.indexOf('画扇'));//13
18 // console.log(str1.indexOf('如',6));//-1
19 // console.log(str1.indexOf('如',3));//4
20 // console.log(str1.indexOf('如',4));//4
21 // console.log(str1.lastIndexOf('生'));//1
22 // console.log(str1.lastIndexOf('画'));//13
23 // console.log(str1.lastIndexOf('画',3));//-1
24 // console.log(str1.lastIndexOf('画',2));//-1
25 // console.log(str1.lastIndexOf('画',13));//13
26 // console.log(str1.lastIndexOf('画',14));//13
27
28 var str4 = '123123123';
29 // console.log(str4.indexOf('2'));//1
30 // console.log(str4.lastIndexOf('2'));//7
31
32 // console.log('120' > '2');//false
33
34 /*
35 for(var i =0; i<str3.length; i++){
36 console.log(str3.charCodeAt(i));
37 }
38 */
39
40 //// var str1 = '人生若只如初见,何事秋风悲画扇。';
41 // console.log(str1.substring(0,2));//人生
42 // console.log(str1.substring(2));//若只如初见,何事秋风悲画扇。
43 // console.log(str1.slice(1,3));//生若
44 // console.log(str1.slice(-3,-1));//画扇
45 // console.log(str1.slice(-5));//风悲画扇。
46 // alert(typeof(str1.slice(5,3)));//String
47
48 var str5 = 'Hello Word';
49 // console.log(str5.toUpperCase());//HELLO WORD
50 // console.log(str5.toLowerCase());//hello word
51 var str6 = 'www.hanyuluoxuan.com';
52 // console.log(str6.split('.'));//["www", "hanyuluoxuan", "com"]
53 // console.log(str6.split('.',1));//["www"]
54 // console.log(str6.split(''));//["w", "w", "w", ".", "h", "a", "n", "y", "u", "l", "u", "o", "x", "u", "a", "n", ".", "c", "o", "m"]
55
56 var arr1 = ["www", "hanyuluoxuan", "com"];
57 console.log(arr1.join());//www,hanyuluoxuan,com
58 console.log(arr1.join(''));//wwwhanyuluoxuancom
59 console.log(arr1.join('雨轩'));//www雨轩hanyuluoxuan雨轩com
60
61 /**
62 * 字符串操作:
63 * 1、str.length; --> 字符串的长度
64 * 2、str.charAt(num); --> 获取某个位置的字符
65 * 3、str.charCodeAt(num);-->获取某个位置上字符的Unicode编码
66 * 0~9 --> 48~57
67 * a~z --> 97~122
68 * A~Z --> 65~90
69 * 4、String.fromCharCode(var1,var2...);-->返回Unicode码对应的字符
70 *
71 * 5、str.indexOf('char-or-string');-->返回该字符或者字符串在其字符串中的位置(返回的是找到的第一个字符的位置)
72 * 6、str.indexOf('char-or-string',number);-->返回该字符或者字符串在其字符串中的位置,查找的起始位置从number开始,如果没有找到就返回-1。
73 *
74 * 7、str.lastIndexOf();-->和str.indexOf();功能一样,就是查找时,查找范围的增长方向不一样,indexOf往number增长的方向寻找,lastIndexOf()往number减少的方向增长。
75 *
76 * 8、字符串比较:> <
77 * -->比较时,只比较字符串中首个字符的Unicode编码的大小
78 *
79 * 9、截取字符串:[indexA,indexB)含前不含后
80 * str.substring(indexA,indexB); -->返回截取的字符串
81 * 1)当indexA>indexB时,会自动调整成适合的位置,即由小到大。
82 * 2)当自由一个参数时,则从该处往后开始截取。
83 * 3)当indexA为负数时,负数为0.
84 * 4)字符串不包含indexA的字符。
85 *
86 * str.slice(indexA,indexB);
87 * 1)indexA和indexB严格由大到小(不遵循则没有返回值,其返回值的类型为String)
88 * 2)当indexA和indexB为负数时,则从字符从的后面开始截取。
89 *
90 * 10、转换大小写(针对英文)
91 * str.toUpperCase();//转化成大写。
92 * str.toLowerCase();//转化成小写。
93 *
94 * 11、字符串分成数组:
95 * str.split('separator',number);//返回值为数组
96 * 1)参数separator:String类型,字符串分割的条件
97 * 2)参数number:number类型,分割成数组的长度(可选参数)
98 * 3)分割条件不会成为数组的成员
99 * 4)当分割的条件为空字符时,字符串按字符分割
100 *
101 * 数组拼接:
102 * 12、arr.join();//返回字符串
103 * 1)当不带参数时,生成的字符串由','连接。
104 * 2)当参数为空字符时,生成的字符串则不含','。
105 * 3)当参数为某个或某些字符串时,则生成字符串时,就会带上该字符或字符串。
106 */
107 };
108 </script>
109 </head>
110 <body>
111 </body>
112 </html>