基本引用类型String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!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>
    <script>
        /*
        1.字符串操作方法
        ---concat()  拼接;直接用 + 加号更省事
        ---slice()   截取(开始下标,结束下标)不包含结束索引值
        ---substrig() 截取 同slice
        ---substr()   截取 (开始下标,返回的字符数)
         */
        /*
        2.字符串位置方法
        ---indexOf()       从字符串开头查找
        ---lastIndexOf()    凑够末尾
        搜索传入的字符串,并返回位置(如果没找到,则返回-1)
         */
 
        //实例,查找所有指定字符的位置
        let stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
        let positions = new Array ();
        let pos = stringValue.indexOf("e");
 
        while(pos > -1) {
            positions.push(pos);
            pos = stringValue.indexOf("e", pos + 1);
        }
        console.log(positions);
 
        /*
        3.字符串包含方法
        ---startsWith()  开始的字符是否是要检索的元素
        ---endsWith()
        ---includes()
 
        是否包含某个字符,返回boolean类型
         */
 
 
        /*
        4.一些其他实用的方法
            trim()
                ---删除前后所有的空格
            repeat()
                ---表示要将字符串复制多少次
            padStart()和padEnd()
                ---分别在原有字符串前后,复制元素   
            toLowerCase()和toLocaleLowerCase()
            toUpperCase()和toLocaleUpperCase()
                ---大小写转换(推荐使用后者,地区特定的转换方法)   
        */
 
        /*
        5.字符串模式匹配方法
            match()
                ---等同于正则RegExp对象的exec()
            search()
                ---返回模式第一个匹配到的位置索引(没找到就返回-1)
            replace()
                ---查找并替换
            split()
                ---根据指定元素,分割字符,返回数组
            localeCompare()
                ---判断字符串先后(按照字母表顺序)       
        */
        let text = "cat, bat, sat, fat";
        let pattern = /.at/;
        let matches = text.match(pattern);
        console.log(matches.index); // 0
        console.log(matches[0]); // cat
        console.log(pattern.lastIndex); // 0
 
        let searchT = text.search(/at/);
        console.log(searchT);  // 1
 
        let result = text.replace("at", "ond");
        console.log(result); // cond, bat, sat, fat
        let result2 = text.replace(/at/g, "ond");
        console.log(result2); // cond, bond, sond, fond
     
    </script>
</body>
</html>

 

posted @   小白咚  阅读(61)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示