• JavaScript中的String


    1、基本类型String

    1. var str ="helloworld";
    要记住:保存的是Unicode字符,一旦创建便不可变
     

    2、引用类型String

    1. var strObj =newString("hello world");
     
    要点:

    字符方法:

    1.     alert (strObj.charAt(0));// 'h'
    2.     alert (strObj.charCodeAt(1));// 101 即 'e' 的 unicode编码
    3.     alert(strObj[1]);  // 'e', ES5
     
     

    字符串方法:

    1.     var str2 = strObj.concat(" ","china");// "hello world china";
    2.     alert(strObj);// "hello world";
     
        slice() & substr() & substring()
        一个参数(起始位置):
    1.     alert(strObj.slice(3));// "lo world";
    2.     alert(strObj.substring(3));// "lo world";
    3.     alert(strObj.substr());// "lo world";
     
        
        两个参数(起始位置,终点位置 || 长度):
    1.     alert(strObj.slice(3,7));// "lo w";  从下标3开始,到下标7之前 
    2.     alert(strObj.substring(3,7));// "hel" ;从下标3, 到下标7之前    
    3.     alert(strObj.substr(3,7));  // "lo worl" 从下标3开始,长度为7
     
     
        第二个参数<0(起始位置,终点位置 || 长度 ,策略不同):
    1.     alert(strObj.slice(3,-3));// "lo wo"; 第二个参数 -3 被转换成 -3 + str.length = 8; 来对待。
    2.     alert(strObj.substring(3,-3));// "hel"; 第二个参数 -3 被转换成 0,因为 第二个参数小于第一个参数,然后它们要互换位置。
    3.     alert(strObj.substr(3,-3));// ""(空字符串),它会将第一个参数 3 + str.length ,然后将 第二个参数-3 转换成 0.
     
        

    字符串位置方法(每次调用只匹配一次,函数返回匹配的位置):

    1.     alert(strObj.indexOf("o"));//4 从前往后
    2.     alert(strObj.lastIndexOf("o"));//7 从后往前
    3.     
    4.     alert(strObj.indexOf("o",6));// 7 忽略位置6以前的(即使匹配)
    5.     alert(strObj.lastIndexOf("o",6));// 4 忽略位置6以后的(即使匹配)
     
        

    trim()方法(删除前置和后置的空格,中间空格不删除):

    1.     var strValue ="              hello world             ";
    2.     alert(strValue.trim());// “hello world”
     
        

    字符串大小写转换:

    1.     alert(strObj.toLowerCase());//"hello world"
    2.     alert(strObj.toUpperCase());// "HELLO WORLD";
    3.     alert(strObj.toLocaleLowerCase());// "hello world“
    4.     alert(strObj.toLocaleUpperCase());// ”HELLO WORLD“
     
     

    模式匹配:

        match:
    1.     var text ="cat, bat, sat, rat";
    2.     var matches = text.match(/.at/);
    3.     alert(matches.index);// 0
    4.     alert(matches[0]);// cat
    5.     alert(matches.lastIndex);// 0
     
     
        search():
    1.     var pos = text.search(/at/);
    2.     alert(pos);// 1 返回第一个匹配的位置
     
       
        replace();     
        
    1. var result1 = text.replace("at","ond");// "cond, bat, sat, rat";
     
        注意:只替换第一个匹配的位置,所以用此方法无法消除字符串中的空格。
      
    1.   var result2 = text.replace(/at/g,"ond");// ”cond, bond, sond, rond“;
     
        消除字符串的所有空格:
      
    1.   text.replace(/s/g,"");//用 ”“(空字符串 )替换 所有的 空格,制表符,换行。    
     
        
    1. var a ="hellod a sad asdsa dsa das dsa dsa dsa ";
    2. console.log(a.replace(/s/g,""));//hellodasadasdsadsadasdsadsadsa VM205:3
    3. var a ="hellod a sad asdsa dsa das dsa dsa dsa ";
    4. console.log(a.replace(" ",""));//helloda sad asdsa dsa das dsa dsa dsa
     
        
        replace()方法的第二个参数也可以是一个函数,这个函数有三个参数(模式的匹配项,模式匹配项在字符串中的位置,原始字符串)
        
    1. function htmlEscape(text){
    2.         return text.replace(/[<>"&]/g,function(match, pos, originalText){
    3.             switch(match){
    4.                 case"<":
    5.                         return"<";
    6.                 case">":
    7.                         return">";
    8.                 case"&":
    9.                         return"&";
    10.                 case""":
    11.                         return""";
    12.             }
    13.         });
    14.     }    
     
        

        split() ,将字符串分隔,返回分隔后组成的数组

    1.     var colorText ="red,blue,green,yellow";
    2.     var c1 = colorText.split(",");//["red","blue","green","yellow"];
    3.     var c2 = colorText.split(”,“,2);//["red","blue"]; 第二个参数返回的数组的大小。
     
     

        localeCompare(),比较字符串

    1.     var strVal ="yellow";
    2.     alert(strVal.localeCompare("black"));// 1
    3.     alert(strVal.localeCompare("yellow"));// 0
    4.     alert(strVal.localeCompare("zoo"));// -1 或其他负数
     
     
     
     





  • 相关阅读:
    eharts入门篇一
    手机侧滑导航栏
    用js+cookie实现商城的购物车功能
    实现文字底部居中
    超出两行或三行显示省略号
    clear-fix清除浮动的两种写法
    sass学习入门篇(三)
    如何回答面试中问到的Hibernate和MyBatis的区别
    设计模式之--单例模式
    设计模式之---工厂模式
  • 原文地址:https://www.cnblogs.com/iceseal/p/3858811.html
Copyright © 2020-2023  润新知