1.翻转字符串
function reverseString(str) {
return str.split("").reverse().join("");
}``
2.字符串中出现最多次数的字符
function findMaxDuplicateChar(str) {
var cnt = {}, //用来记录所有的字符的出现频次
c = ''; //用来记录最大频次的字符
for (var i = 0; i < str.length; i++) {
var ci = str[i];
if (!cnt[ci]) {
cnt[ci] = 1;
} else {
cnt[ci]++;
}
if (c == '' || cnt[ci] > cnt[c]) {
c = ci;
}
}
console.log(cnt)
return c;
}
3.判断回文字符串
```javascript
function palindrome(str) {
var reg = /[\W\_]/g;
var str0 = str.toLowerCase().replace(reg, "");
var str1 = str0.split("").reverse().join("");
return str0 === str1;
}
4.随机生成字符串
function generateRandomAlphaNum(len) {
var rdmString = "";
for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
return rdmString.substr(0, len);
}
5.去除字符串空格
export function trim (str = '', isGlobal = false) {
let result;
result = str.replace(/(^\s+)|(\s+$)/g, '');
if (isGlobal) {
result = result.replace(/\s/g, '');
}
return result;
}
来源:CSDN
作者:无声的code
链接:https://blog.csdn.net/weixin_40542544/article/details/103824626