WHY IE AGAIN? - string.charAt(x) or string[x]?
近期今天在写一个“删除字符串中反复字符串”的函数,代码例如以下:
开门见山,重点
string.charAt(index) 取代 string[index]
function removeReapeatStrings1(str) { var str = trim(str); var len = str.length; var once = str; if (len !== 0) { var fromindex = 1; var index = -1; var i = 0; for (index = str.indexOf(str[0], fromindex); (index !== -1) && (2 * index <= len); ) { // check the characters between 1 and index for (i = 1; i < index; ++i) { if (str[i] !== str[index + i]) { break; } } // if we found a unique string, stop for exit function if (i === index) { once = str.slice(0, index); break; } // not found look for a same character as the first character of str fromindex = index + 1; index = str.indexOf(str[0], fromindex); } } return once; } alert(removeReapeatStrings1('北京')); alert(removeReapeatStrings1('北京北京')); alert(removeReapeatStrings1('大兴大地大兴大地'));
上面代码在非IE浏览器,执行正常。分别alert 北京,北京,大兴大地,但在IE下,奇迹发生了alert 北京, 北京北京。 大兴大地大兴大地。
왜?どうして?
于是各种调试(我用的是简单直观地alert调试法。不要见笑)
最后发现 str[0] 在万恶的IE下,居然是undefined!?所学知识在脑中不断闪现。似乎记起来在《High Performance Javascript》最后几章有这种提示:不要用简短表达式取代原生函数(假设有对应的)。天哪?果断所有的string[index],所实用string.charAt(index)取代。
正确兼容IE和非IE代码例如以下:
function removeReapeatStrings1(str) { var str = trim(str); var len = str.length; var once = str; if (len !== 0) { var fromindex = 1; var index = -1; var i = 0; for (index = str.indexOf(str.charAt(0), fromindex); (index !== -1) && (2 * index <= len); ) { // check the characters between 1 and index for (i = 1; i < index; ++i) { if (str.charAt(i) !== str.charAt(index + i)) { break; } } // if we found a unique string, stop for exit function if (i === index) { once = str.slice(0, index); break; } // not found look for a same character as the first character of str fromindex = index + 1; index = str.indexOf(str.charAt(0), fromindex); } } return once; }
贴一段文字。from http://stackoverflow.com/questions/5943726/string-charatx-or-stringx
There are two ways to access an individual character in a string. The first is the
charAt
method:return 'cat'.charAt(1); // returns "a"
The other way is to treat the string as an array, where each index corresponds to an individual character:
return 'cat'[1]; // returns "a"
The second way (treating the string as an array) is not part of ECMAScript 3; it's a JavaScript and ECMAScript 5 feature (and not supported in all browsers).
In both cases, attempting to set an individual character won't work. Trying to set a character through charAt results in an error, while trying to set a character via indexing does not throw an error, but the string itself is unchanged.
So, as you might have figured out by now, charAt()
is
better from a compatibility perspective.
String.charAt() is the standard and it works in all the browsers. In non-IE browsers you may use bracket notation to access characters but IE doesn't support it. (Not sure whether they have implemented that with the latest versions).
If somebody really wants to use bracket notication. It's wise to convert the string to char array in order to make it compatible with any browser.
面对字符串,请谦卑地使用charAt吧。理由1、正确性
2、兼容性
全篇完结。
相关文章
- [TypeScript] Statically Type String Literals with Template Literal Types in TypeScript
- [ES6] 11. String Templates
- [Typescript] 111. Hard - String Join
- [Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)
- Error: String or binary data would be truncated. The statement has been terminated
- Json string to object debug - json字符串转Java对象的处理调试
- C#中如何比较2个string类型的日期大小?
- 成功解决CatBoostError: Invalid type for cat_feature cat_features must be integer or string, real number
- String StringBuffer StringBuilder 区别
- 成功解决CatBoostError: Invalid type for cat_feature cat_features must be integer or string, real number
- 常见Java面试题 String 类的常用方法都有那些?
- 已解决TypeError: eval() arg 1 must be a string, bytes or code object
- 已解决pymssql.OperationalError: (8152, b‘String or binary data would be truncated.DB-Lib error message
- 带你掌握Redis数据类型:string和Hash
- Java堆、栈和常量池以及相关String的详细讲解(经典中的经典)
- python 时间戳 datetime string 转换
- stl中String类的实现
- 自己写string类(C++)