zoukankan      html  css  js  c++  java
  • substring() slice() substr()的区别联系

    例如:var str='q1207526854'

    str.substring(form,to):从字符串里截取下标为form到下标为to的字符串(不包括to对应的字符)alert(str.substring(2,6))   // 2075
      当form>to时,substring会把较小的参数作为第一参数     alert(str.substring(6,2))   //2075
      并且当其中某个参数值为负数时,substring会直接将负数转成0。   alert(str.substring(6,-2))   //q12075
     
    str.slice(star,end):从字符串里抽取下标为star到下标为end的字符串(不包括end对应的字符) alert(str.slice(2,6))   //2075
        当star>end时,slice会返回空字符串   alert(str.slice(6,2))   // null
        并且当其中某个参数值为负数时,slice会将此参数与字符串长度相加的结果作为参数 alert(str.slice(6,-2))   // 268
        且slice可以对数组操作。
     
     
    str.substr(star,length):从下标为star的字符串开始,截取长度为length的一个子字符串  alert(str.substr(2,6))   // 207526
       当star为负数时,substr会将此参数与字符串长度相加的结果作为参数   alert(str.substr(-6,2))   // 75
       当end为负数时,返回空字符串。 alert(str.substr(6,-2))   // null 
  • 相关阅读:
    HDU 3835 R(N)
    HDU 2498 Digits
    HUST 1027 Enemy Target!
    【POJ 3714】 Raid
    【POJ 2965】 The Pilots Brothers' refrigerator
    【POJ 2054】 Color a Tree
    【POJ 1328】 Radar Installation
    【POJ 3190】 Stall Reservations
    【POJ 3614】 Sunscreen
    【BZOJ 3032】 七夕祭
  • 原文地址:https://www.cnblogs.com/jsingleegg/p/4959256.html
Copyright © 2011-2022 走看看