字符串大小写转换通用函数

此生再无相见时 提交于 2019-11-27 04:47:21


// 方法一
const bigCamel = (s) => {
let empty = " \t\r\n",
result = "";
for (let i = 0; i < s.length; i++) {
if(!empty.includes(s[i])) {
if (empty.includes(s[i-1]) || i === 0) {
result += s[i].toUpperCase()
} else {
result += s[i]
}
}
}
return result;
}
//方法二
const Camel = (str, opt) => {
return str.split(" ")
.filter(item => {
return item.length > 0
}).map(item => {
return opt === "lower" ?
item[0].toLowerCase() + item.substring(1) :
item[0].toUpperCase() + item.substring(1)
}).join(" ")
};

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!