Javascript count_chars
Returns info about what characters are used in input
Example 1
Running
1.count_chars("Hello World!", 1);
Could return
1."Hd e!lWor"
function count_chars( str, mode ) {
// Returns info about what characters are used in input
//
// version: 810.1317
// discuss at: http://phpjs.org/functions/count_chars
// + original by: Ates Goral (http://magnetiq.com)
// + tweaked by: Jack
// + bugfixed by: Onno Marsman
// * example 1: count_chars("Hello World!", 1);
// * returns 1: "Hd e!lWor"
var histogram = new Object(), tmp_arr = new Array();
var key, i, code, mode, strl = 0;
var argc = arguments.length;
if (argc == 1) {
mode = 0;
}
mode_even = (mode & 1) == 0;
if (mode_even) {
for (i = 1; i < 256; ++i) {
histogram[i] = 0;
}
}
str += '';
strl = str.length;
for (i = 0; i < strl; ++i) {
code = str.charCodeAt(i);
if (code in histogram) {
++histogram[code];
} else {
histogram[code] = 1;
}
}
if (mode > 0) {
for (key in histogram) {
if (histogram[key] == 0 != mode_even) {
delete histogram[key];
}
}
}
if (mode < 3) {
return histogram;
} else {
for (key in histogram) {
tmp_arr.push(String.fromCharCode(key));
}
return tmp_arr.join("");
}
}
|
HTML code for linking to this page:
Related in same category :
-
-
-
-
-
-
-
-
-
-
-
-
-
|
|