這是用String本身的方法實現(xiàn)(es6的新增方法不太熟悉筷狼,基本上es5的);
- 使用String的match方法
- 用正則匹配返回是的匹配到的字符串的數(shù)組
- 用string匹配鳞芙,則返回一個類似
["a", index: 2, input: "dfas"]
- 沒有匹配觉既,則返回null
function getMostChar(str) {
//出現(xiàn)的字符
var most_char = '';
var char = '';
var char_arr = [];
//出現(xiàn)的次數(shù)
var most_count = 0;
var count = 0;
var count_arr = [];
var len = str.length;
if (len > 1) {
(function() {
for (var i = 0; i < len; i++) {
char = str[i];
var flag = char_arr.some(function(item, index) {
return item == char;
})
if (!flag) {
char_arr.push(char);
var pattern = new RegExp(char, 'g');
var result = str.match(pattern);
if (result) {
count = result.length;
}
count_arr.push(count);
}
if (most_count < count) {
most_count = count;
most_char = char;
}
}
})()
} else {
char_arr.push(str);
most_count = len;
most_char = str;
}
console.log('擁有的字符的種類:' + char_arr, ';出現(xiàn)最多次數(shù)的字符:' + most_char, '事秀;出現(xiàn)的次數(shù)的數(shù)組:' + count_arr, '灾梦;出現(xiàn)最多次數(shù):' + most_count);
}
- indexOf 和
do{}while()
- indexOf() 返回字符出現(xiàn)的第一次的位置,沒有匹配則返回-1
- idnexOf(str,startpos) 的有兩個參數(shù)
- str 匹配的字符
- startpos
{type number}
設(shè)置從字符串的開始匹配位置(包含這個startpos)
- do{}while() 錯誤的也得執(zhí)行一次
function getMostChar(str) {
//出現(xiàn)的字符
var most_char = '';
var char = '';
var char_arr = [];
//出現(xiàn)的次數(shù)
var most_count = 0;
var count = 0;
var count_arr = [];
//字符串出現(xiàn)的位置
var pos = -1;
var len = str.length;
if (len > 1) {
(function() {
for (var i = 0; i < len; i++) {
char = str[i];
var flag = char_arr.some(function(item, index) {
return item == char;
})
if (!flag) {
char_arr.push(char);
do {
count++;
pos = str.indexOf(char, pos+1);
} while(pos>-1)
if(i===0){
count = count-1;
}
count_arr.push(count);
}
if(most_count < count){
most_count = count;
most_char = char;
}
//重置為下次遍歷做準備
count = 0;
// 已經(jīng)檢測過不在檢測槐雾,對pos設(shè)置為當前位置
pos = i+1;
}
})()
} else {
char_arr.push(str);
most_count = len;
most_char = str;
}
console.log('pos: ' + pos, '夭委;擁有的字符的種類:' + char_arr, ';出現(xiàn)最多次數(shù)的字符:' + most_char, '募强;出現(xiàn)的次數(shù)的數(shù)組:' + count_arr, '株灸;出現(xiàn)最多次數(shù):' + most_count);
}
while 類似
function getMostChar(str) {
//出現(xiàn)的字符
var most_char = '';
var char = '';
var char_arr = [];
//出現(xiàn)的次數(shù)
var most_count = 0;
var count = 0;
var count_arr = [];
//字符串出現(xiàn)的位置
var pos = 0;
var len = str.length;
if (len > 1) {
(function() {
for (var i = 0; i < len; i++) {
char = str[i];
var flag = char_arr.some(function(item, index) {
return item == char;
})
if (!flag) {
char_arr.push(char);
while(pos>-1) {
count++;
pos = str.indexOf(char, pos+1);
}
count_arr.push(count);
}
if(most_count < count){
most_count = count;
most_char = char;
}
//重置為下次遍歷做準備
count = 0;
pos = i+1;
}
})()
} else {
char_arr.push(str);
most_count = len;
most_char = str;
}
console.log('pos: ' + pos, ';擁有的字符的種類:' + char_arr, '擎值;出現(xiàn)最多次數(shù)的字符:' + most_char, '慌烧;出現(xiàn)的次數(shù)的數(shù)組:' + count_arr, ';出現(xiàn)最多次數(shù):' + most_count);
}
- 用search 方法實現(xiàn)鸠儿,其實邏輯同indexOf是一樣的屹蚊。只不過要利用字符串的截取方法。
function getMostChar(str) {
//出現(xiàn)的字符
var most_char = '';
var char = '';
var char_arr = [];
//出現(xiàn)的次數(shù)
var most_count = 0;
var count = 0;
var count_arr = [];
//字符串出現(xiàn)的位置
var pos = 0;
var sub_str = '';
var sub_str_pos = 0;
var len = str.length;
if (len > 1) {
(function() {
for (var i = 0; i < len; i++) {
char = str[i];
var flag = char_arr.some(function(item, index) {
return item == char;
})
if (!flag) {
char_arr.push(char);
var pattern = new RegExp(char, 'g');
while (sub_str_pos > -1) {
count++;
sub_str = str.slice(pos+1);
sub_str_pos = sub_str.search(pattern);
pos = (pos + 1) + sub_str_pos;
}
count_arr.push(count);
}
if (most_count < count) {
most_count = count;
most_char = char;
}
//重置為下次遍歷做準備
pos = i + 1;
count = 0;
sub_str = '';
sub_str_pos = 0;
}
})()
} else {
char_arr.push(str);
most_count = len;
most_char = str;
}
console.log('pos: ' + pos, '捆交;擁有的字符的種類:' + char_arr, '淑翼;出現(xiàn)最多次數(shù)的字符:' + most_char, ';出現(xiàn)的次數(shù)的數(shù)組:' + count_arr, '品追;出現(xiàn)最多次數(shù):' + most_count);
}
- replace ,這是最最簡單的,最好理解冯丙。
function getMostChar(str) {
//出現(xiàn)的字符
var most_char = '';
var char_arr = [];
//出現(xiàn)的次數(shù)
var most_count = 0;
var count = 0;
var count_arr = [];
var new_char = str[0];
var len = str.length;
while (str) { // 實際上是這樣子的 while(str !== '')
char_arr.push(new_char);
var pattern = new RegExp(new_char, 'g');
str = str.replace(pattern, '');
count = len - str.length;
count_arr.push(count);
if (most_count < count) {
most_count = count;
most_char = new_char;
}
// 重置
len = str.length;
new_char = str[0];
}
console.log('擁有的字符的種類:' + char_arr, '肉瓦;出現(xiàn)最多次數(shù)的字符:' + most_char, ';出現(xiàn)的次數(shù)的數(shù)組:' + count_arr, '胃惜;出現(xiàn)最多次數(shù):' + most_count);
}
- split 這個方法使用起來實際上是把字符串變成數(shù)組泞莉,那就不是String方法。所以沒做下去船殉。
這個有什么好玩性了鲫趁,可以檢測作者是怎么用詞,檢測他用詞利虫。
例如挨厚,我找汪曾祺的《異秉》,測試:
var str = '小說的內(nèi)容';
getMostCharSort(str);
function getMostCharSort(str) {
var kv_arr = [];
//出現(xiàn)的字符
var char = str[0];
var most_char = '';
var char_arr = [];
//出現(xiàn)的次數(shù)
var most_count = 0;
var count = 0;
var count_arr = [];
var len = str.length;
while (str) {
var pattern = new RegExp(char, 'g');
str = str.replace(pattern, '');
if (!(/\,|\糠惫。|\:|\“|\”|\疫剃?|\!|\s|\硼讽、|\—|\(|\)/ig.test(char))) {
char_arr.push(char);
count = len - str.length;
kv_arr.push({
count: count,
char: char
});
count_arr.push(count);
if (most_count < count) {
most_count = count;
most_char = char;
}
}
// 重置
len = str.length;
char = str[0];
}
console.log('擁有的字符的種類:' + char_arr);
console.log('出現(xiàn)最多次數(shù)的字符:' + most_char);
console.log('出現(xiàn)的次數(shù)的數(shù)組:' + count_arr, );
console.log('出現(xiàn)最多次數(shù):' + most_count);
des(kv_arr)
show(kv_arr);
}
function show(arr) {
var table = document.createElement('table');
var table_html = '<tr><td>字符</td><td>次數(shù)</td></tr>'
+ '<tr><td>'+ '字的種類:' +'</td><td>' + arr.length + '</td></tr>';
arr.forEach(function(item, index) {
table_html = table_html + ('<tr ><td>' + item.char + '</td><td> ' + item.count + '</td></tr>');
})
table.innerHTML = table_html;
document.body.appendChild(table);
}
function des(arr){
for(var i = 0; i < arr.length; i++){
for(var j = arr.length-1; j > i; j--) {
if(arr[i].count <= arr[j].count) {
var des_count = arr[j].count;
var des_char = arr[j].char;
arr[j].count = arr[i].count;
arr[j].char = arr[i].char;
arr[i].count = des_count;
arr[i].char = des_char;
}
}
}
}
我們將算法改進巢价,不在用冒泡。
function getMostCharSort(str) {
var kv_arr = [];
//出現(xiàn)的字符
var char = str[0];
//出現(xiàn)的次數(shù)
var most_count = 0;
var less_count = 0;
var center_count = 0;
var center_index = 0;
var count = 0;
var len = str.length;
while (str) { // 實際上是這樣子的 while(str !== '')
var pattern = new RegExp(char, 'g');
str = str.replace(pattern, '');
// 去掉一些特殊的符號
if (!(/\,|\壤躲。|\:|\“|\”|\城菊?|\!|\s|\碉克、|\—|\(|\)/ig.test(char))) {
count = len - str.length;
if (count >= most_count) {
kv_arr.unshift({
count: count,
char: char
});
center_index ++;
center_count = kv_arr[center_index];
most_count = count;
if(kv_arr.length == 1){
less_count = count;
}
} else {
if (count > center_count) {
for (var j = center_index - 1; j > 0; j--) {
if (count < kv_arr[j]) {
kv_arr.splice(j, 0, {
count: count,
char: char
})
}
center_count++
center_count = kv_arr[center_index];
}
} else {
if (count <= less_count) {
kv_arr.push({
count: count,
char: char
})
less_count = count;
} else if(count > less_count) {
var less_index = kv_arr.length;
for (var i = center_index + 1; i < less_index; i++) {
if (count > kv_arr[i]) {
kv_arr.splice(i-1,0,{
count: count,
char: char
})
}
}
}
}
}
}
len = str.length;
char = str[0];
}
show(kv_arr);
}
function show(arr) {
var table = document.createElement('table');
var table_html = '<tr><td>字符</td><td>次數(shù)</td></tr>' +
'<tr><td>' + '字的種類:' + '</td><td>' + arr.length + '</td></tr>';
arr.forEach(function(item, index) {
table_html = table_html + ('<tr ><td>' + item.char + '</td><td> ' + item.count + '</td></tr>');
})
table.innerHTML = table_html;
document.body.appendChild(table);
}