Java教程

JavaScript的String对象相关方法

本文主要是介绍JavaScript的String对象相关方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        /*var str="hello world";
        // console.log(str[1]);
        console.log(str.charAt(4));
        //检测@在email中出现的位置
        var email="marry.mail@sohu.com";
        console.log(email.indexOf("o"));
        console.log(email.lastIndexOf("."));*/
        var str="hello world";
        //截取orl
        /*console.log(str.slice(7,10));
        console.log(str.slice(1));
        //str.slice(8)    //str.slice(4,9)
        console.log(str.slice(-7,-2));*/
        /*console.log(str.substring(-7,5));//(0,5)
        console.log(str.substring(2,-5));//(0,2)*/
        //str.substring(6,9)
        console.log(str.substr(6,3));
        console.log(str.substr(-5,4));//(6,4)
        console.log(str.substr(3,-4));
        </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        //获取扩展名
        var url="http://baidu.com/index.txt";
        function getFileFormat(url){
            //获取.在url中出现的位置
            var pos=url.lastIndexOf(".");
            return url.substr(pos);
        }
        var formatName=getFileFormat(url);
        var picFormat=getFileFormat("1231434.jpg");
        console.log(formatName);
        console.log(picFormat);
    </script>
</body>
</html> 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        var str='welcome-to-beijing';
        //使用split将str转换为数组
        var arr=str.split("-");
        var date='2016/05/05';
        //["welcome","to","bejing"]
        var dateArr=date.split("/");
        console.log(arr);
        console.log(dateArr);
        //替换
        var tel='010-62971268,400-100-9098,010-86789889';
        var newTel=tel.replace(',',"  ");
        console.log(tel);
        console.log(newTel);
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        /*var str="hello world";
        var upper=str.charAt(6).toUpperCase();
        // console.log(str.toUpperCase());
        var str2="WELCOME";
        console.log(str2.toLowerCase());*/
        //border-left-color borderLeftColor    //驼峰形式
        //margin-bottom
        function camelback(str){
            //通过-这个分隔符将str拆分成数组
            var arr=str.split("-"),newStr=arr[0];
            //console.log(arr);//["border","left","color"]
            //newStr="border";
            for(var i=1,len=arr.length;i<len;i++){
                var word=arr[i];
                //将每一个单词的首字母转换为大写,连接剩余字符
                newStr+=word.charAt(0).toUpperCase()+word.substr(1);
                //console.log(newStr);    //Left Color L C
                //newStr=borderLeftColor
            }
            return newStr;
        }
        //var camelFormat=camelback("border-left-color");
        var camelFormat=camelback("get-element-by-id");
        console.log(camelFormat);
    </script>
</body>
</html>

 

这篇关于JavaScript的String对象相关方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!