Recently, I am writing a function "deleting strings that repeat strings". The Code is as follows:
Key Points
String. charAt (index) replaces 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 indexfor (I = 1; I <index; ++ I) {if (str [I]! ==Str [index + I]) {break ;}// if we found a unique string, stop for exit functionif (I === index) {once = str. slice (0, index); break;} // not found look for a same character as the first character of strfromindex = index + 1; index = str. indexOf (str [0], fromindex) ;}return once;} alert (removeReapeatStrings1 ('beijing'); alert (removeReapeatStrings1 ('beijing ')); alert (removeReapeatStrings1 ('great things of the day '));
The above code runs normally in non-IE browsers, including alert Beijing, Beijing, and Daxing Earth. However, in IE, the miracle happened to alert Beijing, Beijing, and Daxing Earth.
Why? Why does it happen?
So various debugging (I am using the simple and intuitive alert debugging method, don't laugh)
Finally, we found that str [0] is undefined under the Internet Explorer !? What I learned is constantly popping up in my mind. It seems that in the last few chapters of High Performance Javascript, there is a prompt: do not replace native functions with short expressions (if appropriate ). God? Use string. charAt (index) instead of string [index.
Correct compatibility with IE and non-IE code is as follows:
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 indexfor (i = 1; i < index; ++i) {if (str.charAt(i) !== str.charAt(index + i)) {break;}}// if we found a unique string, stop for exit functionif (i === index) {once = str.slice(0, index);break;}// not found look for a same character as the first character of strfromindex = index + 1;index = str.indexOf(str.charAt(0), fromindex);}}return once;}
Paste a paragraph of text from http://stackoverflow.com/questions/5943726/string-charatx-or-stringx
There are two ways to access an individual character in a string. The first ischarAt
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.
Use charAt in a humble manner in the face of strings. Reason
1. correctness
2. Compatibility
The entire article is complete.