• 字符串方法


    3.字符串位置方法:

    indexOf() 从头开始找,找不到,返回-1

    lastIndexOf() 从尾……

    如果有第二个参数,表示从字符串中的哪个位置开始搜索

    var stringValue = "hello world";
    alert(stringValue.indexOf("o")); //4
    alert(stringValue.lastIndexOf("o"));//7  
    View Code
    var stringValue = "hello world";
    alert(stringValue.indexOf("o",6)); //7
    alert(stringValue.lastIndexOf("o",6));//4  
    View Code

    注意:有第二个参数时,从第6个位置开始,又前往后找,在索引值为7的位置找到,直接返回7。千万不要返回1,索引值不需要重新编排

    var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
    var positions = new Array();
    var pos = stringValue.indexOf("e");//3
    
    while(pos>-1){
        positions.push(pos);
        pos = stringValue.indexOf("e",pos+1);//找下一个e
    }
    
    alert(positions) //3,24,32,35,52

    4.trim()方法

    去掉前后的空格,返回去空格后的字符串副本

    trimleft(),trimright()分别用于删除字符串开头和末尾的空格

    5.大小写转换

    toLowerCase(),toLocaleLowerCase() ,toUpperCase(),toLocaleUpperCase();

    toLocaleLowerCase()和toLocaleUpperCase()是针对特定地区的实现。(不理解)

    6.模式匹配方法

     match()方法,只接受一个参数(正则或者RegExp):

    var text="cat,sat,bat,fat";
    var pattern = /.at/;
    
    var matches = text.match(pattern);//查找与整个模式匹配的字符串
    alert(matches.index);
    alert(matches[0]);
    alert(pattern.lastIndex);
    View Code

    search()方法,返回字符串中第一个匹配项的索引,没有则返回-1;

    7.localeCompare()方法

    比较两个字符串,并返回-1,0,1

    var stringValue = "yellow";
    alert(stringValue.localeCompare("brick"));//1
    alert(stringValue.localeCompare("yellow"));//0
    alert(stringValue.localeCompare("zoo"));//-1

    注意:因为yellow排在brick的后面,即yellow>brick,所以返回1;stringValue是被比较的对象,与参数比较

  • 相关阅读:
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 菜单与按钮
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 布局
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    雷林鹏分享:jQuery EasyUI 数据网格
    高考后的BB
  • 原文地址:https://www.cnblogs.com/n2meetu/p/6733823.html
Copyright © 2020-2023  润新知