转载自己博客园中的文章 http://www.cnblogs.com/IcemanZB/p/4134441.html
先看下不兼容的写法,若我想获取某个字符串的第几位
var str='aavvvcc'; console.info(str[0]);
这种写法 在IE 7以下的浏览器都不兼容,以下提供浏览器全兼容的方式
var str='aavvvcc'; console.info(str.charAt(1));
使用charAt() 就可以获取字符串中某个字符,并且全部兼容
补充 经过测试 substring 效率更高。
var s="",i=1000000; while(i>0){ s+="a"; i--; } console.time("charAt"); console.log(s.charAt(10)); console.timeEnd("charAt"); // charAt 28.18ms console.time("substring"); console.log(s.substring(9,10)); console.timeEnd("substring"); // substring 2.34ms