Home > Web Front-end > JS Tutorial > JS string traversal, interception, output, and calculation operations

JS string traversal, interception, output, and calculation operations

php中世界最好的语言
Release: 2018-04-19 13:49:57
Original
3296 people have browsed it

This time I will bring you JS string traversal, interception, output, and calculation operations. What are the precautions for JS string traversal, interception, output, and calculation operations? The following is a practical case, let's take a look.

<!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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JS implements simple four arithmetic operations

JS implements drop-down menu login registration pop-up window

Angular2 development component step-by-step explanation

The above is the detailed content of JS string traversal, interception, output, and calculation operations. 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