JavaScript學習之路——字符串操作

<!doctype html>
<html>

    

<head>
            
    <meta charset="utf-8">
            <title>測試</title>
            
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
            
    <script>
        var str = "Hello ";

        //顯示長度
        console.log(str.length);


        //返回指定位置的字符
        var result = str.charAt(0);
        console.log(result);
        
        //返回指定位置字符編碼
        result = str.charCodeAt(0);
        console.log(result);
        
        //利用字符編碼創建一個字符
        result = String.fromCharCode(72);
        console.log(result);
        
        //連接字符串
        result = str.concat("World !");
        console.log(result);
       
        //檢測是否含有某元素,返回第一次出現的位置,找不到返回-1;可以指定開始查找的起始位置
        //lastindexOf()用法一樣,從後查找
        result = str.indexOf("o");
        console.log(result);
        result = str.indexOf("l", 0);
        console.log(result);
        result = str.indexOf("l", 3);
        console.log(result);
       
        //切片操作,從開始位置截取幾個,省略第二個參數,則截取初始位置之後所有;可以爲負數
        str = "aaabbbcccdddeee";
        result = str.slice(0, 3);
        console.log(result);
       
        //substring(),不接受負數
        result = str.substring(0, 3);
        console.log(result);
        
        //拆分數組,拆分後 result 爲數組,如果傳遞空串,則將所有字符拆分爲數組元素
        str = "abc,def,ghi";
        result = str.split(",");
        console.log(result);

        //大小寫轉換
        result = str.toUpperCase();
        console.log(result);
        result = str.toLowerCase();
        console.log(result);

    </script>
        </head>

    

<body>
            
</body>

</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章