方法一:

    stringObject.lastIndexOf(substr):查找返回substr字符(串)在string里面最后一次出现的下标,找不到的话返回-1

    stringObject.substr(startindex[,length]):截取字符串,若无length参数时,直接截取到字符串末尾

<script>
    var filename = "javascript.html";
    var index = filename.lastIndexOf(".");
    var suffix = filename.substr(index+1);
    document.write(suffix);
</script>

方法二:

    stringObject.substring(startIndex,[stopIndex]) 截取字符串 (startIndex:开始下标 stop 可缺省 表示要结束的下标[该下标的字符不会被截取!!!])

<script>
    var filename = "javascript.html";
    var index = filename.lastIndexOf(".");
    var suffix = filename.substring(index+1);
    document.write(suffix);
</script>

方法三:

    stringObject.split(sep) 分割字符串,将字符串以指定的分隔符分割为一个数组

<script>
    var filename = "javascript.html";
    var spl = filename.split(".");
    document.write(spl[spl.length-1]);
</script>

标签: none

添加新评论