zoukankan      html  css  js  c++  java
  • 【JS】引用类型之String

    由于String的方法过多,这里只列举一下常用的但又不是很熟的

    1、replace()

    var text = "cat, bat, sat, fat"; 
    
    var result = text.replace("at", "ond");
    alert(result);    //"cond, bat, sat, fat"   替换第一个
    
    result = text.replace(/at/g, "ond");
    alert(result);    //"cond, bond, sond, fond"   //替换全部
    
    result = text.replace(/(.at)/g, "word ($1)");
    alert(result);    //word (cat), word (bat), word (sat), word (fat)    //使用特殊的字符序列替换,字符序列有($$、$&、$`、$'、$n、$nn),具体用法需要时查
    
    function htmlEscape(text){
        return text.replace(/[<>"&]/g, function(match, pos, originalText){
            switch(match){
                case "<":
                    return "&lt;";
                case ">":
                    return "&gt;";
                case "&":
                    return "&amp;";
                case "\"":
                    return "&quot;";
            }             
        });
    }
    
    alert(htmlEscape("<p class=\"greeting\">Hello world!</p>")); //&lt;p class=&quot;greeting&quot;&gt;Hello world!&lt;/p&gt;

    2、localeCompare()

    基于时区(地区)的比较函数

    var stringValue = "yellow";       
    alert(stringValue.localeCompare("brick"));  //1
    alert(stringValue.localeCompare("yellow")); //0
    alert(stringValue.localeCompare("zoo"));    //-1
    
    //preferred technique for using localeCompare()
    function determineOrder(value) {
        var result = stringValue.localeCompare(value);
        if (result < 0){
            alert("The string 'yellow' comes before the string '" + value + "'.");
        } else if (result > 0) {
            alert("The string 'yellow' comes after the string '" + value + "'.");
        } else {
            alert("The string 'yellow' is equal to the string '" + value + "'.");
        }
    }
    
    determineOrder("brick");
    determineOrder("yellow");
    determineOrder("zoo");

    3、slice()/substring()/substr()

    var stringValue = "hello world";
    alert(stringValue.slice(3));        //"lo world"
    alert(stringValue.substring(3));    //"lo world"
    alert(stringValue.substr(3));       //"lo world"
    alert(stringValue.slice(3, 7));     //"lo w"
    alert(stringValue.substring(3,7));  //"lo w"
    alert(stringValue.substr(3, 7));    //"lo worl"
    
    alert(stringValue.slice(-3));         //"rld"
    alert(stringValue.substring(-3));     //"hello world"
    alert(stringValue.substr(-3));        //"rld"
    alert(stringValue.slice(3, -4));      //"lo w"
    alert(stringValue.substring(3, -4));  //"hel"
    alert(stringValue.substr(3, -4));     //"" (empty string)
  • 相关阅读:
    小白_开始学Scrapy__原理
    python zip()函数
    前端工程精粹(一):静态资源版本更新与缓存
    HTML 5 History API的”前生今世”
    常见的几个js疑难点,match,charAt,charCodeAt,map,search
    前端安全须知
    Html5游戏框架createJs组件--EaselJS(二)绘图类graphics
    Html5游戏框架createJs组件--EaselJS(一)
    github基本用法
    jquery ajax中事件的执行顺序
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/3014632.html
Copyright © 2011-2022 走看看