【JS】substring和substr的区别

语法:
stringObject.substr(start,length)
stringObject.substring(start,stop)
 

    var mystr = 'abcdefg';   


    console.log(  mystr.substr(5,2)  );
    console.log(  mystr.substring(5,2)  );


    console.log(  mystr.substr(0,3)  );
    console.log(  mystr.substring(0,3)  );

    console.log(  mystr.substr(3)  );
    console.log(  mystr.substring(3)  );



输出结果是:

net.html:1 fg
net.html:2 cde

net.html:3 abc
net.html:4 abc

net.html:5 defg
net.html:6 defg

看出区别了吗?

唯一结果不一样的是1,2组,由此可以知道:

substr()函数 使用start作为开始点,end作为结束点。

而substring()函数 使用start 和end 两者中的较小值作为子字符串的起始点

猜你喜欢

转载自blog.csdn.net/sphinx1122/article/details/83721340