字符串String对象方法汇总

字符串String对象方法汇总
这里写图片描述
这里写图片描述
其中一些方法的举例及应用,持续更新……………

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>字符串方法</title>
    </head>
    <body>
        <script type="text/javascript">
            /*
             * slice()、substr()、substring()
             */
            var string = "hello world";
            //传递一个参数的情况
            document.write(string.slice(1) + "<br>"); //"ello world"
            document.write(string.substr(1) + "<br>"); //"ello world"
            document.write(string.substring(1) + "<br>"); //"ello world"

            //传递两个参数的情况
            document.write(string.slice(1, 7) + "<br>"); //"ello w"
            document.write(string.substring(1, 7) + "<br>"); //"ello w"
            document.write(string.substr(1, 7) + "<br>"); //"ello w0"共7位

            /*
             * 字符串位置方法
             * :indexOf()和 lastIndexOf()。
             * 这两个方法都是从 一个字符串中搜索给定的子字符串,然后返子字符串的位置
             * (如果没有找到该子字符串,则返回-1)。
             * 这两个方法的区别在于:indexOf()方法从字符串的开头向后搜索子字符串
             * 而 lastIndexOf()方法 是从字符串的末尾向前搜索子字符串
             */
            var str = "hello world";
            //传递一个参数
            var value1 = str.indexOf("o");
            document.write(value1 + "<br>"); //4
            var value2 = str.lastIndexOf("o");
            document.write(value2 + "<br>"); //7
            //传递两个参数
            //从第六个位置开始向后搜索
            var value3 = str.indexOf("o", 6);
            document.write(value3 + "<br>"); //7
            //从第六个位置开始向前搜索
            var value4 = str.lastIndexOf("o", 6);  
            document.write(value4 + "<br>"); //4

            /*
             * 可以通过循环调用 indexOf()或 lastIndexOf()来找到所有匹配的子字符串
             */
            var str2 = "Hello Averyone, I am Wangwei. I am from Yunnan, a beautifual place";
            //创建数组保存数值
            var position = new Array();
            var pos = str2.toLowerCase().indexOf("a"); //6
           //alert(pos1);
            while(pos > -1) {
                position.push(pos);
                pos = str2.toLowerCase().indexOf("a", pos+1);
            }
            document.write(position); //6, 18, 22, 32, 44, 48, 52, 58, 63

            //去除字符串前后空格方法trim()
            var str3 = "   Hello World  ";
            var trimStr = str3.trim();
            document.write("<br>");
            document.write(trimStr); //Hello World

            //比较字符串位置的localeCompare()方法
            //返回1, -1或0
            document.write("<br>");
            var str4 = "yellow";
            alert(str4.localeCompare("brain"));  //1
            //用函数实现
            function localeCom(str1, str2) {
                var result = str1.localeCompare(str2);
                if(result < 0) {
                    document.write("The string " + " " +str1 + " "  + " comes before the string "+ str2 + "<br>");
                }else if(result > 0) {
                    document.write("The string " + " " + str1 + " " +  " comes after the string "+ str2 + "<br>");
                }else {
                    document.write("The string " + " " + str1 + " " + " is equal to " + str2 + "<br>");
                }
            }
            localeCom("yellow", "brain");
            localeCom("brain", "yellow");
            localeCom("yellow", "yellow");

            /*
             * charCodeAt()和fromCharCode()方法
             *
             *获取数字部分:Unicode编码值范围在[48~57]之间即为数字
             *获取英文字母部分:Unicode编码值范围在[65~90],以及[97~122]之间即为英文字母
             *获取中文部分,(122, )
             */
            //charCodeAt()方法
            function get(str) {
                var saveNum = new Array(); //接收数字
                var saveLowerLetter = new Array(); //接收大写英文字母
                var saveUpperLetter = new Array(); //接收小写英文字母
                var saveChina = new Array(); //接收中文
                for(var i=0; i<str.length; i++) {
                    if((str[i].charCodeAt()) >= 48 && (str[i].charCodeAt()) <= 57) {
                        saveNum.push(str[i]);
                    }else if((str[i].charCodeAt()) >= 65 && (str[i].charCodeAt()) <= 90) {
                        saveUpperLetter.push(str[i]);
                    }else if((str[i].charCodeAt()) >= 97 && (str[i].charCodeAt()) <= 122) {
                        saveLowerLetter.push(str[i]);
                    }else if ((str[i].charCodeAt()) > 122) {
                        saveChina.push(str[i]);
                    }
                }
                document.write(saveNum + "<br>");
                document.write(saveLowerLetter + "<br>");
                document.write(saveUpperLetter + "<br>");
                document.write(saveChina + "<br>");
            }
            get("我是1234喵喵喵lalalla嘿嘿HAHAHA1872");
            //fromCharCode()方法
            //String.fromCharCode()函数用于从一些Unicode字符值中返回一个字符串
            //属于string对象
            document.writeln(String.fromCharCode(65, 66, 67, 68, 69, 70)); //ABCDEF
            document.write("<br>");
            document.writeln(String.fromCharCode(101, 104, 97, 98, 122, 100)); //ehabzd
            document.write("<br>");
            document.writeln(String.fromCharCode(20013, 22269)); //中国

        </script>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="{CHARSET}">
        <title>字符串操作方法</title>
    </head>
    <body>
        <script type="text/javascript">
            var str = new String();
            str = "Hello World!";
            //anchor(name)方法: 创建 HTML 锚,name为必选属性。
            document.write(str.anchor("myAnchor") + "<br>");
            //big()方法:用大号字体显示字符串。
            document.write(str.big() + "<br>");
            //blink()方法,显示闪动字符串。
            document.write(str.blink() + "<br>");
            //bold(): 使用粗体显示字符串。
            document.write(str.bold() + "<br>");
            //charAt(): 返回在指定位置的字符。
            document.write(str.charAt(1) + "<br>"); //e
            //charCodeAt(): 返回在指定的位置的字符的 Unicode 编码。
            document.write(str.charCodeAt(1)+ "<br>"); //101
            //concat(): 连接字符串。
            document.write(str.concat(" I am wangwei")+"<br>"); 
            //fixed(): 以打字机文本显示字符串。
            document.write(str.fixed()+"<br>"); 
            //fontcolor(): 使用指定的颜色来显示字符串。
            document.write(str.fontcolor("red")+"<br>"); 
            //fontsize(): 使用指定的尺寸来显示字符串。
            document.write(str.fontsize(20)+"<br>"); 
            //fromCharCode(): 从字符编码创建一个字符串。
            document.write(String.fromCharCode(101, 102, 103)+ "<br>"); //efg
            //italics(): 使用斜体显示字符串。
            document.write(str.italics()+"<br>"); 
            //link(): 将字符串显示为链接。
            document.write(str.link()+"<br>"); 
            //mach(): 使用 match() 来检索一个字符串。
            document.write(str.match("world") + "<br>"); //null
            document.write(str.match("World") + "<br>"); //World
            //mach(): 使用 match() 来检索一个正则表达式的匹配。
            var str2 = "1 plus 2 equal 3";
            document.write(str2.match(/\d+/g) + "<br>"); //1,2,3
            //replace() – 用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串
            document.write(str.replace(/World/, "world") + "<br>"); //Hello world
            //search(): 检索与正则表达式相匹配的值。
            //总是从字符串的开始进行检索,这意味着它总是返回 stringObject 的第一个匹配的位置。
            //对大小写敏感
            document.write(str.search(/World/) + "<br>"); //6
            //small()   使用小字号来显示字符串。
            document.write(str.small() + "<br>"); 
            //split()   把字符串分割为字符串数组。
            document.write(str.split("W") + "<br>"); 
            //strike()  使用删除线来显示字符串。
            document.write(str.strike() + "<br>"); 
            //sub()     把字符串显示为下标。
            document.write("Hello" + str.sub() + "<br>"); 
            //substr()  从起始索引号提取字符串中指定数目的字符。
            document.write(str.substr(0, 5) + "<br>"); //Hello
            //substring()   提取字符串中两个指定的索引号之间的字符。
            document.write(str.substring(0, 5) + "<br>"); //Hello(不包括结束位置)
            //sup()     把字符串显示为上标。
            document.write("Hello" + str.sup() + "<br>"); 
            //toLocaleLowerCase()   把字符串转换为小写。
            document.write(str.toLocaleLowerCase() + "<br>"); //hello world
            //toLocaleUpperCase()   把字符串转换为大写。
            document.write(str.toLocaleUpperCase() + "<br>"); //HELLO WORLD
            //toLowerCase()     把字符串转换为小写。
            //toUpperCase()     把字符串转换为大写。
            //toString()    返回字符串。
            document.write(str.toString() + "<br>"); //Hello World!
            //valueOf()     返回某个字符串对象的原始值。
            document.write(str.valueOf() + "<br>"); //Hello World!

        </script>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_36595013/article/details/82190691