JavaScript 中的字符串

JavaScript 字符串

什么是字符串?

  • 字符串是用来存储和处理文本数据的
    • 比如:'一句话''a'

字符串的创建方式

  • 字面量方式进行创建

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        // 字面量创建方式
        // 双引号包含
        var a = "123";
        var b = "false";
        console.log(typeof a);
        console.log(typeof b);
      </script>
    </html>
    

    在这里插入图片描述

  • 通过new String的方式进行创建

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        // 通过 new String 的方式创建
        var a = new String("123");
        console.log(typeof a); // 对象
      </script>
    </html>
    

    在这里插入图片描述

字符串的长度 length

  • 返回字符串的长度

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
        console.log(a.length); // 11
      </script>
    </html>
    

字符串的方法

  • charAt返回指定位置上的内容,开始位置也是 0

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
        var b = a.charAt(4);
        console.log(b); // o 返回第一个o
      </script>
    </html>
    
  • charCodeAt返回指定位置上的 Unicode 值

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
        var b = a.charCodeAt(1);  // e
        // 返回字符串的 Unicode 值
        console.log(b); // 101 
      </script>
    </html>
    
  • concant连接两个或多个字符串

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
        var b = "你";
        var c = a.concat(b, "好!");
        console.log(c); // hello world你好!
      </script>
    </html>
    
  • fromCharCode将 Unicode 转换为字符串

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = String.fromCharCode(101);
        console.log(a); // e
      </script>
    </html>
    
  • indexOf返回指定字符在字符串中出现的位置,找不到返回-1

  • lastIndexOf返回指定字符在字符串中最后一次出现的位置,找不到返回-1

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
        
        var b = a.indexOf("o");
        console.log(b); // 4
    
        var c = a.lastIndexOf("l");
        console.log(c); // 9
      </script>
    </html>
    
  • localeCompare用本地特定的方式比较两个字符串的顺序,相同返回0,不同返回-1

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello";
        var b = "hello";
        var c = "world";
        console.log(a.localeCompare(b)); // 0
        console.log(a.localeCompare(c)); // -1
      </script>
    </html>
    
  • replace找到指定字符,并替换

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        // 找到指定的字符并进行替换
        var a = "hello world";
    
        var b = a.replace("hello", "");
    
        console.log(a); // hello world
        console.log(b); //  world
        console.log(b.length); // 6
        // 注意 world 前面的空格还在
      </script>
    </html>
    
  • search检索与正则表达式相匹配的值,能够检索到是0,检索不到为-1

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
    
        console.log(a.search("hello")); //0
        console.log(a.search("hellox")); // -1
      </script>
    </html>
    
  • slice

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello";
    
        // slice 第一个参数是开始的字符,第二个参数是剪切的个数(个数不是索引值)
        console.log(a.slice("h", 2)); // he
      </script>
    </html>
    
  • split以指定的方式将字符串进行切割,并且返回数组

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body></body>
      <script>
        var a = "hello world";
        var c = "admin@13456";
        var b = a.split(" ");
        var x = c.split("@");
    
        console.log(b); // ['hello', 'world']
        console.log(x); // ['admin', '13456']
      </script>
    </html>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值