Home > Article > Web Front-end > JS string traversal, interception, output, and calculation operations
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('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>
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!