Str的方法

  1 <!DOCTYPE html>
  2 <html>
  3     <head>
  4         <meta charset="UTF-8">
  5         <title></title>
  6         <script type="text/javascript">
  7             //创建一个字符串
  8             var str = "Hello Atguigu";
  9             
 10             /*
 11              * 在底层字符串是以字符数组的形式保存的
 12              * ["H","e","l"]
 13              */
 14             
 15             /*
 16              * length属性
 17              *     - 可以用来获取字符串的长度
 18              */
 19             //console.log(str.length);
 20             //console.log(str[5]);
 21             
 22             /*
 23              * charAt()
 24              *     - 可以返回字符串中指定位置的字符
 25              *     - 根据索引获取指定的字符    
 26              */
 27             str = "中Hello Atguigu";
 28             
 29             var result = str.charAt(6);
 30             
 31             /*
 32              * charCodeAt()
 33              *     - 获取指定位置字符的字符编码(Unicode编码)
 34              */
 35             
 36             result = str.charCodeAt(0);
 37             
 38             /*
 39              * String.formCharCode()
 40              *     - 可以根据字符编码去获取字符
 41              */
 42             result = String.fromCharCode(0x2692);
 43             
 44             /*
 45              * concat()
 46              *     - 可以用来连接两个或多个字符串
 47              *     - 作用和+一样
 48              */
 49             result = str.concat("你好","再见");
 50             
 51             /*
 52              * indexof()
 53              *     - 该方法可以检索一个字符串中是否含有指定内容
 54              *     - 如果字符串中含有该内容,则会返回其第一次出现的索引
 55              *         如果没有找到指定的内容,则返回-1
 56              *     - 可以指定一个第二个参数,指定开始查找的位置
 57                      
 58              * 
 59              * lastIndexOf();
 60              *     - 该方法的用法和indexOf()一样,
 61              *         不同的是indexOf是从前往后找,
 62              *         而lastIndexOf是从后往前找
 63              *     - 也可以指定开始查找的位置
 64              */
 65             
 66             str = "hello hatguigu";
 67             
 68             result = str.indexOf("h",1);
 69             
 70             result = str.lastIndexOf("h",5);
 71             
 72             /*
 73              * slice()
 74              *     - 可以从字符串中截取指定的内容
 75              *     - 不会影响原字符串,而是将截取到内容返回
 76              *     - 参数:
 77              *         第一个,开始位置的索引(包括开始位置)
 78              *         第二个,结束位置的索引(不包括结束位置)
 79              *             - 如果省略第二个参数,则会截取到后边所有的
 80              *         - 也可以传递一个负数作为参数,负数的话将会从后边计算
 81              */
 82             str = "abcdefghijk";
 83             
 84             result = str.slice(1,4);
 85             result = str.slice(1,-1);
 86             
 87             /*
 88              * substring()
 89              *     - 可以用来截取一个字符串,可以slice()类似
 90              *     - 参数:
 91              *         - 第一个:开始截取位置的索引(包括开始位置)
 92              *         - 第二个:结束位置的索引(不包括结束位置)
 93              *         - 不同的是这个方法不能接受负值作为参数,
 94              *             如果传递了一个负值,则默认使用0
 95              *         - 而且他还自动调整参数的位置,如果第二个参数小于第一个,则自动交换
 96              */
 97             
 98             result = str.substring(0,1);
 99             
100             /*
101              * substr()
102              *     - 用来截取字符串
103              *     - 参数:
104              *         1.截取开始位置的索引
105              *         2.截取的长度
106              */
107             
108             str = "abcdefg";
109             
110             result = str.substr(3,2);
111             
112             /*
113              * split()
114              *     - 可以将一个字符串拆分为一个数组
115              *     - 参数:
116              *         -需要一个字符串作为参数,将会根据该字符串去拆分数组
117              */
118             str = "abcbcdefghij";
119             
120             result = str.split("d");
121             
122             /*
123              * 如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素
124              */
125             result = str.split("");
126             
127             //console.log(Array.isArray(result));
128             //console.log(result[0]);
129             console.log(result);
130             
131             
132             str = "abcdefg";
133             
134             /*
135              * toUpperCase()
136              *     - 将一个字符串转换为大写并返回
137              */
138             result = str.toUpperCase();
139             
140             str = "ABCDEFG";
141             
142             /*
143              * toLowerCase()
144              *     -将一个字符串转换为小写并返回
145              */
146             result = str.toLowerCase();
147             
148             //console.log(result);
149             
150             
151         </script>
152     </head>
153     <body>
154     </body>
155 </html>

 

posted @ 2020-05-07 16:07  全情海洋  阅读(356)  评论(0编辑  收藏  举报