Home > Web Front-end > JS Tutorial > body text

Sample code for JS string statistical operations [traversal, interception, output, calculation] (picture)

黄舟
Release: 2017-03-27 14:32:31
Original
1825 people have browsed it

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(&#39;p&#39;)); //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 + &#39;=&#39; + obj[key].count + &#39; &#39;); // a=4 b=3 c=4 d=2 f=1 g=1 h=1 
}
</script>
</body>
</html>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template