字符串常用方法案例

1.和諧敏感字

1.replace方法
 <textarea class="txt" name="" id="" cols="30" rows="10"></textarea>
    <p><button class="btn">留言</button></p>
    <div class="show"></div>
    <script>
        var txt = document.querySelector('.txt');
        var btn = document.querySelector('.btn');
        var show = document.querySelector('.show');
        btn.onclick = function () {
            var val = txt.value;
            val = val.replace('馬化騰', '###')

            show.innerText = val;
        }
2.正則方法
  <textarea class="txt" name="" id="" cols="30" rows="10"></textarea>
    <p><button class="btn">留言</button></p>
    <div class="show"></div>
    <script>
        var txt = document.querySelector('.txt');
        var btn = document.querySelector('.btn');
        var show = document.querySelector('.show');
        btn.onclick = function () {
            var val = txt.value;
            var reg = /馬化騰|麻花|話話|騰訊/g;//正則
            val = val.replace(reg, function (str) {
                var x = '';
                for (var i in str) {
                    x += '*'

                }
                return x;

            })
            show.innerText = val;
        }

    </script>

2.案例:計(jì)算各種類型字符在字符串中出現(xiàn)的次數(shù)

方法1:
  var str = "ahG%%%Gmfsfs342s#$#%";
        //各種類型:小寫字母 大寫字母 數(shù)組 特殊符號(hào)
        function getCount(str) {
            var ch = "";
            var xxCount = 0;
            var dxCount = 0;
            var numCount = 0;
            var otherCount = 0;
            //獲取每一個(gè)字符
            for (var i = 0; i < str.length; i++) {
                ch = str.charAt(i);
                //判斷字符的種類
                if (ch >= "a" && ch <= "z") {
                    xxCount++;
                } else if (ch >= "A" && ch <= "Z") {
                    dxCount++;
                } else if (ch >= "0" && ch <= "9") {
                    numCount++;
                } else {
                    otherCount++;
                }
            }
            console.log(xxCount, dxCount, numCount, otherCount);//8 2 3 7
        }
        getCount(str);


3.統(tǒng)計(jì)一個(gè)字符在str中的個(gè)數(shù)

    var str = "iyifaaaaAAAAw832646%%%";
        str=str.toLowerCase();
        var obj={};
        for(var i=0;i<str.length;i++){
            var letter=str[i];//單個(gè)字符
            if(obj[letter]){
                obj[letter]=obj[letter]+1;
            }else{
                obj[letter]=1;
            }
    
        }
        console.log(obj);
//輸出結(jié)果
2: 1
3: 1
4: 1
6: 2
8: 1
%: 3
a: 8
f: 1
i: 2
w: 1
y: 1

4.字符串轉(zhuǎn)數(shù)組 并獲取其中的值

var str = "http://www.baidu.com?uname=lichune&pwd=123&age=19&phone=110";
        //獲取密碼
        var arr = str.split("&");
        //將字符串變成數(shù)組
        console.log(arr);// ["http://www.baidu.com?uname=lichune", "pwd=123", "age=19", "phone=110"]
        console.log(arr[1].split("=")[1]);//123

    //獲取所有等號(hào)后面的值
        var search = str.split("?")[1].split("&");
        //console.log(search);
        for (var i = 0; i < search.length; i++) {
            console.log(search[i].split("=")[1]);
        }

5.arr =["Apple","orange","banana","red","blue"]; 查找這個(gè)數(shù)組中 包含A或a的字符串 的個(gè)數(shù) (3)

 var arr = ["Apple", "orange", "banana", "red", "blue"];
        function fn(arr) {
            var count = 0;
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].indexOf("a") != -1 || arr[i].includes("A")) {
                    count++;
                }
            }
            console.log(count);//3
        }
        fn(arr);

6.QQ號(hào)驗(yàn)證

要求: 1、有沒(méi)有輸入
2、輸入的是不是數(shù)字
3、不能有0在前面
4、不能是小數(shù)
5、輸入的數(shù)字必須在5位以上、10位以內(nèi)

    qqNum: <input id="qqNum" type="text">
    <button id="checkQQ">驗(yàn)證</button>
    <script>

        var qqNum = document.getElementById('qqNum');
        var checkQQ = document.getElementById('checkQQ');
        //給checkQQ按鈕綁定點(diǎn)擊事件
        checkQQ.onclick = function () {     
            //qq驗(yàn)證
            //獲取用戶輸入的QQ號(hào)內(nèi)容
            var qqVal = qqNum.value;
            //前后空格去掉
            qqVal = qqVal.trim();
            //判斷有沒(méi)有輸入
            if (qqVal === "") {
                alert("qq號(hào)不能為空")
            } else if (isNaN(qqVal)) {//輸入的不是數(shù)字
                alert("必須是數(shù)字");
            } else if (qqVal.charAt(0) === "0") {//0不能再前面
                alert("首位不能為0");
            } else if (qqVal.indexOf(".") != -1) {//不能是小數(shù)
                alert("不能輸入小數(shù)");
            } else if (qqVal.length < 5 || qqVal.length > 10) {//輸入數(shù)字為5-10為
                alert("數(shù)字長(zhǎng)度在5-10位");
            } else {
                alert("可以使用");
            }
        }

  </script>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容