This article mainly introduces JSString Statistics Operation, combined with examples, it analyzes the traversal, interception, output, calculation and other related operation skills of javascript strings and Notes, friends in need can refer to the following
The examples in this article describe JS characters String statistics operation. Share it with everyone for your reference. The details are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>JS字符串</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <script> var str = "aaddaabbcdddefg"; console.log(str.charAt(7)); //b 没有返回空不是null console.log(str.indexOf('p')); //1 没有返回-1 var obj = {}; for (var i = 0; i < str.length; i++) { var v = str.charAt(i); if (obj[v] && obj[v].value == v) { obj[v].count++; } else { obj[v] = {}; obj[v].count = 1; obj[v].value = v; } } console.log(obj); //true //obj = {a:object,b:object,c:object} for (key in obj) { document.write(obj[key].value + '=' + obj[key].count + ' '); // a=4 b=3 c=4 d=2 f=1 g=1 h=1 } </script> </body> </html>
Record each item in the string and record the number. The operation effect is as follows:
The above is the detailed content of Sample code for JS string statistical operations [traversal, interception, output, calculation] (picture). For more information, please follow other related articles on the PHP Chinese website!