• 关于String的属性和方法


     

    一、关于String的属性和方法

    String作为JavaScript的内置函数,String.prototype继承于Object.prototype,同时String.prototype上内置了很多方法:

    nchor()、big()、blink()、bold()、charAt()、charCodeAt()、codePointAt()、concat()、String()、endsWith()、fixed()、
    
    fontcolor()、fontsize()、includes()、indexOf()、italics()、
    lastIndexOf()、length、link()、localeCompare()、match()、
    
    normalize()、padEnd()、padStart()、repeat()、replace()、search()、slice()、small()、split()、startsWith()、strike()、sub()、substr()、
    
    substring()、sup()、toLocaleLowerCase()、toLocaleUpperCase()、toLowerCase()、toString()、toUpperCase()、trim()、trimLeft()、
    
    trimRight()、valueOf()、[Symbol.iterator]()、__proto__: Object
    var str = "abc123ABC";
    var str1 = "Concat1";
    var str2 = "Concat2";

    1、字符串的length属性

    console.log(str.length); // 9


    2、字符串的方法
    a. concat

    console.log(str.concat(str1, str2)); // abc123ABCConcat1Concat2


    ==> 返回新的字符,不改变原字符串

    b. slice(start,end)

    console.log(string.slice(0, 3)); // abc ==> 下标[0, 3)
    console.log(string.slice(-3)); // ABC ==> 倒数第3位到结束
    console.log(string.slice(-6, -3)); // 123 ==> 倒数下标[-6, -3)


    ==> slice(start,end)==>返回一个新的字符串。包括字符串 stringObject 从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符。当没有第二个参数时,截止到最后。

    ==> 当只有一个参数,并且这个参数为负数时,表示从字符串尾部开始,-1表示倒数第一开始,n表示倒数第n开始到最后。


    c. substring(start,end)

    console.log(str.substring(0, 3)); // abc ==> 下标[0, 3)
    console.log(str.substring(-2, 3)); // abc ==> -2转化为0,下标[0, 3)
    console.log(str.substring(-2, -3)); // abc ==> -2和-3转化为0,为空字符串
    // substring和slice差不多,只是不支持负数,转换为0


    d. substr(start,lenght)

    console.log(str.substr(0, 5)); // abc12
    console.log(str.substr(-3, 5)); // ABC
    console.log(str.substr(-3)); // ABC
    console.log(str.substr(-3, -5)); // ABC


    //和上面两个不同的是substr的第二个参数表示截取字符串的长度,如果只有一个参数和slice相同,如果第二个参数为为负数转换为0

    //还要注意的是,String.slice() 与 Array.slice() 相似。


    对以上三个方法的总结:
    1、只接受两个参数,返回新的字符串,不改变原字符串
    2、slice ==> 参数可以为负
    subString ==> 参数都不可以为负,自动转换为0
    subStr ==> 第一个参数可以为负,第二个参数表示的是字符串的长度,不能为负数,自动转换为0

    e. indexOf(string, start)和lastIndexOf(string, start)

    indexOf搜索目标字符串的开始索引,第一个参数只能是字符串,且对大小写敏感,只返回第一个的位置索引; indexOf第二个参数表示从该索引开始搜索,只能为正数,负数自动转化为0。

    lastIndexOf从后往前搜索目标字符串的开始索引,第一个参数只能是字符串,且对大小写敏感,只返回最后一个的位置索引; indexOf第二个参数只能为正数,负数返回结果为-1找不到。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。

    var str = '123abcABC123abcABC123abcABC123abcABC';
    console.log(str.indexOf('abc')); // 3
    console.log(str.indexOf('dddd')); // -1 ==> 找不到
    
    str.indexOf('abc', 6); // 12
    str.indexOf('abc', -6); // 3
    
    str.lastIndexOf('abc', 4); // 3
    str.lastIndexOf('abc', -4); // -1
    str.lastIndexOf('abc', 14); // 12

    f. search(sting/RegExp)

    搜索目标字符串的开始索引,参数可以是字符串或者正则,对大小写敏感,正则可以忽略大小写敏感。 只返回第一个的位置索引,所以即使是全局匹配g也是返回第一个的位置索引; 特别注意:search方法只接收一个参数。

    var str = '123abcABC123abcABC123abcABC123abcABC';
    console.log(str.search('abc')); // 3
    console.log(str.search(/Abc/ig)); // 3
    console.log(str.search(/Abc/ig, 6)); // 3
    console.log(str.search('dddd')); // -1 ==> 找不到

    indexOf:不支持正则,支持指定起始位置开始搜索(负数时转化为0),有lastIndex反向搜索 search:支持正则和字符串,不支持指定起始位置开始搜索

    g. match(searchvalue, regexp)

    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
    该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。

    searchvalue 必需。规定要检索的字符串值。
    regexp 必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。

    var str = '123abcABC123abcABC123abcABC123abcABC';
    console.log(str.match('abc')); // ["abc"]
    console.log(str.match(/abc/)); // ["abc"]
    console.log(str.match(/abc/g)); // ["abc", "abc", "abc", "abc"]
    console.log(str.match(/d+/g)); // ["123", "123", "123", "123"]


    h. replace(str/RegExp, repalcement) 第一个参数时字符串或正则,字符串只能替换第一个,正则可以匹配设置(ig); 第二个参数repalcement如果是字符串那么就只接替换,repalcement可以是函数。

    var str = '123abcABC123abcABC123abcABC123abcABC';
    console.log(str.replace('abc', 'RRR')); // "123RRRABC123abcABC123abcABC123abcABC"
    
    console.log(str.replace(/abc/ig, 'RRR')); // "123RRRRRR123RRRRRR123RRRRRR123RRRRRR"

    i. trim() ==> IE9以下没有这个方法,要做兼容,如下:

    //console.log("trim()==>空格处理:")
    String.prototype.ltrim=function() {
       return this.replace(/^s*/g,"");
    };
    String.prototype.rtrim=function() {
       return this.replace(/s*$/g,"");
    };
    String.prototype.trim=function() {
       return this.replace(/(^s*)|(s*$)/g,"");
    };

    j. 字符串比较

    log(str.localeCompare(str1)); //-1
    log(str1.localeCompare(str)); //1
    log(str.localeCompare(str)); // 0


    k. 字符串大小写转换

    log(str.toUpperCase()); // ABC123ABC
    log(str.toLocaleUpperCase()); // ABC123ABC
    
    log(str.toLowerCase()); // abc123abc
    log(str.toLocaleLowerCase()); // abc123abc

    l. split ==> 将字符串转换为数组

    var arr = str.split('');
    console.log(arr); //["a", "b", "c", "1", "2", "3", "A", "B", "C"]

    join ==> 将数组转换为字符串

    console.log(arr.join('')); // abc123ABC


    二、支持正则表达式的 String 对象的方法

    search、match、replace、split
  • 相关阅读:
    POJ2553 The Bottom of a Graph 强连通分量+缩点
    销售里面最猛的还是传销模式
    **Error 1 The type 'System.Web.Compilation.WCFBuildProvider' is ambiguous: it could come from assembly 'C:\Windows\Micro
    自定义Proxy 来做 rest wcf的客户端 与 WebChannelFactory<>的bug
    你知道吗string GetHashCode方法跟环境是有关的吗?
    查找默认的一个实体如果没有就找第一个的写法
    如何解决WCF REST 中 WebFaultException抛出的异常客户端WebChannelFactory无法处理的问题
    Google 的统计或将导致 IE7已终止操作
    类别进行排序的一个简单分析
    抢夺客户的战争
  • 原文地址:https://www.cnblogs.com/yanmuxiao/p/8674285.html
Copyright © 2020-2023  润新知