• JavaScript的string方法(demo)


      1 <!DOCTYPE html>
      2 <html>
      3     <head>
      4         <meta charset="UTF-8">
      5         <title></title>
      6     </head>
      7     <body>
      8         <script type="text/javascript">
      9         
     10             //charAt(index); 返回指定位置的字符,返回的字符是长度为 1 的字符串,index为字符在字符串中的下标。
     11             var string='hello world'; 
     12             console.log(string.charAt(6));//下标从0开始 
     13             
     14             //concat() 方法用于连接两个或多个字符串。
     15             var string1=string.concat('-lww');
     16             console.log(string1); 
     17             
     18             //concat() 方法用于连接两个或多个字符串。
     19             var string2=string.concat('-lww','kk','!','xiangruding'); 
     20             console.log(string2); 
     21             
     22             //substr() 在字符串中抽取从 start 下标开始的指定数目的字符。
     23             var string4=string.substr(3); 
     24             console.log(string4);//lo world 
     25             
     26             //stringObject.substr(start,length)
     27             //start:必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
     28             //length:可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。
     29             var string8=string.substr(3,7);
     30             console.log(string8);//lo worl
     31             
     32             //substring()
     33             var string5=string.substring(3); 
     34             console.log(string5);//lo world 
     35             
     36             //stringObject.substring(start,stop) 提取字符串中介于两个指定下标之间的字符。
     37             //返回一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。
     38             var string9=string.substring(3,7); 
     39             console.log(string9);//lo w 
     40             
     41             //slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
     42             var string6=string.slice(3); 
     43             console.log(string6)//lo world
     44             
     45             //stringObject.slice(start,end)
     46             //返回一个新的字符串。包括字符串 stringObject 从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符
     47             var string7=string1.slice(3,7); 
     48             console.log(string7);//lo w 
     49             
     50             //indexOf()  返回某个指定的字符串值在字符串中首次出现的位置。 如果要检索的字符串值没有出现,则该方法返回 -1
     51             var string10=string.indexOf('l'); 
     52             console.log(string10);//2 
     53             
     54             //stringObject.indexOf(searchvalue,fromindex) 
     55             //该方法将从头到尾地检索字符串 stringObject,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的开头(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一次出现的位置。stringObject 中的字符位置是从 0 开始的。
     56             //从第6个位置开始搜寻,忽略前面的字符
     57             var string12=string.indexOf('o',6); 
     58             console.log(string12);//7  
     59             
     60             //lastIndexOf()  返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
     61             var string11=string.lastIndexOf('l'); 
     62             console.log(string11);//9 
     63             
     64             
     65             //
     66             var string13='hello world this is a string html you can see it'; 
     67             var position = new Array(); 
     68             var pos=string13.indexOf('i'); 
     69             while(pos>-1){ 
     70                 position.push(pos); 
     71                 pos=string13.indexOf('i',pos+1); 
     72             } 
     73             console.log(position);    //所有i的位置组成的数组    [14, 17, 25, 46]
     74             console.log(position[0]);  //14
     75 
     76 
     77             //trim() 去除字符串左右两端的空格
     78             var string14='    hello world '; 
     79             console.log(string14);  //    hello world 
     80             var string15=string14.trim(); 
     81             console.log(string15);//hello world  
     82 
     83 
     84             //转换大小写方法 
     85             var string16=string.toLocaleUpperCase(); 
     86             console.log(string16);//HELLO WORLD 
     87 
     88             var string17=string.toUpperCase(); 
     89             console.log(string17);//HELLO WORLD 
     90 
     91             var string19=string17.toLocaleLowerCase(); 
     92             console.log(string19);//hello world  
     93 
     94             var string18=string17.toLowerCase(); 
     95             console.log(string18);//hello world 
     96 
     97 
     98             //replace()替换方法 
     99             var string20=string.replace('l','k'); 
    100             console.log(string20);//heklo world将第一个匹配到的l替换成K  
    101             
    102             
    103             //search()方法跟indexOf()有点相似 
    104             var string21=string.search('l'); 
    105             console.log(string21);     //返回第一个l的位置 2  
    106             
    107             
    108             //split()  方法根据指定的分隔符来将字符串分成多个字符串并组成数组 
    109             var string22=string.split(' '); 
    110             console.log(string22);//根据指定的空格的字符串来把字符串分成多个字符串并组成数组['hello','world'] 
    111             
    112             var string23='red,yellow,green,blue'; 
    113             var string24=string23.split(',');
    114             console.log(string24);//['red','yellow','green','blue']; 
    115             var string25=string23.split(',',1);
    116             console.log(string25);//['red'];这个数字1就代表保留一个字符串,要是3的话就保留3个字符串['red,'yellow','blue'];  
    117             
    118             
    119             //localeCompare()
    120             //用本地特定的顺序来比较两个字符串。
    121             //stringObject.localeCompare(target)  
    122             //如果 stringObject 小于 target,则 localeCompare() 返回小于 0 的数。如果 stringObject 大于 target,则该方法返回大于 0 的数。如果两个字符串相等,或根据本地排序规则没有区别,该方法返回 0。
    123             var string26='red'; 
    124             var string27=string26.localeCompare('yellow');
    125             console.log(string27)//-1因为在字母表中y在r的后面 所有未负数-1 
    126             
    127             var string28=string26.localeCompare('red');
    128             console.log(string28);//0因为字母相同 
    129             
    130             var string29=string26.localeCompare('ahh');
    131             console.log(string29) //1
    132             
    133             //1因为字母a在前 
    134             var string30=string26.localeCompare('rff');
    135             console.log(string30); //-1第一个字母相同则比较第二个
    136 
    137         </script>
    138     </body>
    139 </html>
  • 相关阅读:
    Java开发常用知识点总结
    Net实现阿里云开放云存储服务(OSS)
    KEIL MDK-ARM Version 5.26正式版开发工具下载
    【NXP开发板应用—智能插排】4. PWM驱动
    【NXP开发板应用—智能插排】3.驱动GPIO点亮外接LED
    【NXP开发板应用—智能插排】2.初步解析example之GPI
    【NXP开发板应用—智能插排】1.如何使用scp传输文件
    Keil MDK最新版 5.25介绍及下载地址
    springmvc框架helloword
    单例设计模式
  • 原文地址:https://www.cnblogs.com/xiangru0921/p/7100485.html
Copyright © 2020-2023  润新知