-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.js
62 lines (56 loc) · 1.32 KB
/
string.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Author:Toti
* Mail:mr.totihuang@gmail.com
*/
/**
* 编写函数 getNumberStr(1,false,'2','b',3,4,'a')=>'1,2,3'
*/
export function getNumberStr(...args) {
let arr = [];
for (const item of args) {
if (Number(item) <= 3 && Number(item) > 0) {
arr.push(String(item));
}
}
console.log(arr.join(","));
}
/**
* 写一个字符串转换成驼峰的方法?
* 例子:border-bottom-color -> borderBottomColor
*/
export function toCamel(str) {
return str.split("-").reduce((prev, cur) => {
return prev + cur.charAt(0).toUpperCase() + cur.substring(1);
});
};
/**
* 查找字符串中出现最多的字符和个数?
* abbcccddddd -> 字符最多的是d,出现了5次
* @param {string} str
*/
export function countLetter(str) {
return str.split("").reduce((all, name) => {
all[name] ? all[name]++ : (all[name] = 1);
return all;
}, {});
};
/**
* 如何给字符串加千分符?
* 42342342342 -> 42,342,342,342
*/
export function toMoney(str) {
let arr1 = [];
arr1 = str.split("").reverse();
let arr2 = [];
for (var i = 0; i < arr1.length; i += 3) {
arr2.push(arr1[i + 2] + arr1[i + 1] + arr1[i]);
}
return arr2
.reverse()
.join(",")
.replace("undefined", "");
};
/** 增加去除字符串前后空格 */
export function trim(str) {
return str.trim();
}