Blogger Information
Blog 9
fans 0
comment 0
visits 5923
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js中常用的字符串函数、数组函数、分支与循环的理解
不是本人
Original
718 people have browsed it

1 常用的字符串函数

1.1 slice()函数

语法:slice(start,end)。
作用:提取字符串中指定的一部分字符。
参数:start,字符串位置索引,用于指定开始位置;end字符串位置索引,用于指定结束位置。
返回值:返回被开始位置到结束位置的字符串。原字符串不变。
示例:

  1. <script>
  2. let str1 = "I love China, I love Chinese people!";
  3. let res = str1.slice(0, 3);
  4. console.log(str1);
  5. console.log(res);
  6. </script>

1.2 concat

语法:concat(string1[,string2,…])。
作用:连接两个或多个字符串。
参数:string1,要被链接的字符串,可以连接多个字符串,除了第一个字符串,后面是可选参数。
返回值:返回连接后的字符串。
示例:

  1. <script>
  2. let str1 = "hello";
  3. let res = str1.concat(" China");
  4. console.log(str1);
  5. console.log(res);
  6. </script>

1.3 substr

语法:substr(start,length)。
作用:截取字符串中指定的一部分字符。
参数:start,字符串位置索引,用于指定开始位置;length要截取的字符串长度。
返回值:返回截取后的字符串。
示例:

  1. <script>
  2. let str1 = "I love China, I love Chinese people!";
  3. let res = str1.substr(2, 6);
  4. console.log(str1);
  5. console.log(res);
  6. </script>

1.4 trim

语法:trim()。
作用:删除字符串两端的空白符。
参数:strging,待处理的字符串。
返回值:删除两端空白符之后的字符串。
示例:

  1. <script>
  2. let str1 = " hello ";
  3. let res = str1.trim();
  4. console.log(str1);
  5. console.log(res);
  6. </script>

1.5 split()

语法:split(string)。
作用:将字符串用指定的字符串分割成数组。
参数:strging,用来分割原始字符串的“刀”。
返回值:分割之后形成的数组。
示例:

  1. <script>
  2. let str1 = "I love China, I love Chinese people!";
  3. let res = str1.split(" ");
  4. console.log(str1);
  5. console.log(res);
  6. </script>

2 常用的数组函数

2.1 push()

语法:push(item1,[item2,…])。
作用:在数组尾部插入一些新的元素。
参数:item11,待插入的新元素,可插入多个,除了第一个元素,后面是可选参数。
返回值:新数组的长度
示例:

  1. <script>
  2. let arr1 = ["a", "b", "c", "d"];
  3. console.log(arr1);
  4. let res = arr1.push("e", "f");
  5. console.log(arr1);
  6. console.log(res);
  7. </script>

2.2 pop()

语法:pop()。
作用:从数组中删除最后一个元素。
参数:无。
返回值:被删除的元素。
示例:

  1. <script>
  2. let arr1 = ["a", "b", "c", "d"];
  3. console.log(arr1);
  4. let res = arr1.pop();
  5. console.log(arr1);
  6. console.log(res);
  7. </script>

2.3 shift()

语法:pop()。
作用:从数组中删除第一个元素。
参数:无。
返回值:被删除的元素。
示例:

  1. <script>
  2. let arr1 = ["a", "b", "c", "d"];
  3. console.log(arr1);
  4. let res = arr1.shift();
  5. console.log(arr1);
  6. console.log(res);
  7. </script>

2.4 unshift()

语法:unshift(item1,[item2,…])。
作用:在数头部插入一些新的元素。
参数:item11,待插入的新元素,可插入多个,除了第一个元素,后面是可选参数。
返回值:新数组的长度
示例:

  1. <script>
  2. let arr1 = ["a", "b", "c", "d"];
  3. console.log(arr1);
  4. let res = arr1.unshift("e", "f");
  5. console.log(arr1);
  6. console.log(res);
  7. </script>

2.5 splice()

语法:splice(index,howmany,item1,…..,itemX)。
作用:在数组中添加或删除元素。
参数:index指定添加/删除项目的位置;howmany指定删除元素的个数,如果为0则不删除;item1,…..,itemX可选。向数组添加的新项目。
返回值:被删除的元素组成的新数组,如果没有删除元素则返回空数组。
示例:

  1. <script>
  2. let arr1 = ["a", "b", "c", "d"];
  3. // 添加内容
  4. let res = arr1.splice(1, 0, "e", "f", "g");
  5. console.log(arr1);
  6. console.log(res);
  7. console.log("---------------------------------------");
  8. // 删除内容
  9. res = arr1.splice(1, 1);
  10. console.log(arr1);
  11. console.log(res);
  12. </script>

3 分支与循环

3.1 for 循环

  1. <script>
  2. let str1 = "hello";
  3. let text = "";
  4. for (let a = 0; a < str1.length; a++) {
  5. text += str1[a];
  6. }
  7. console.log(text);
  8. </script>

3.2 while 循环

  1. <script>
  2. let str1 = "hello";
  3. let text = "";
  4. let a = 0;
  5. while (a < str1.length) {
  6. text += str1[a];
  7. a++;
  8. }
  9. console.log(text);
  10. </script>

3.3 do-while 循环

  1. <script>
  2. let str1 = "hello";
  3. let text = "";
  4. let a = 0;
  5. do {
  6. text += str1[a];
  7. a++;
  8. } while (a < str1.length - 1);
  9. console.log(text);
  10. </script>

3.4 if分支

  1. <script>
  2. let a = 1;
  3. if (a == 1) {
  4. console.log("a =", a);
  5. }
  6. </script>

3.5 if else 分支

  1. <script>
  2. let a = 2;
  3. if (a == 1) {
  4. console.log("a =", 1);
  5. } else {
  6. console.log("a ≠", 1);
  7. }
  8. </script>

3.6 if else if 分支

  1. <script>
  2. let a = new Date();
  3. d = a.getDay();
  4. if (d == 1) {
  5. console.log("今天星期六");
  6. } else if (d == 2) {
  7. console.log("今天星期天");
  8. } else {
  9. console.log("今天是工作日");
  10. }
  11. </script>

3.7 switch 分支

  1. <script>
  2. let a = new Date();
  3. d = a.getDay();
  4. switch (d) {
  5. case 6:
  6. console.log("今天星期六");
  7. break;
  8. case 0:
  9. console.log("今天星期天");
  10. break;
  11. default:
  12. console.log("今天是工作日");
  13. break;
  14. }
  15. </script>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!