输一个字符串,切换其中字母大小写,
如:输入 `'12aBc34'` 输出 `12AbC34'`
分析
需要判断字母是大写还是小写
- 正则表达式
- `charCodeAt` 获取 ASCII 码(ASCII 码表,可以网上搜索)
性能分析
- 正则表达式性能较差
- ASCII 码性能较好
/**
* @description 切换字母大小写
*/
/**
* 切换字母大小写(正则表达式)
* @param s str
*/
export function switchLetterCase1(s: string): string {
let res = ''
const length = s.length
if (length === 0) return res
const reg1 = /[a-z]/
const reg2 = /[A-Z]/
for (let i = 0; i < length; i++) {
const c = s[i]
if (reg1.test(c)) {
res += c.toUpperCase()
} else if (reg2.test(c)) {
res += c.toLowerCase()
} else {
res += c
}
}
return res
}
/**
* 切换字母大小写(ASCII 编码)
*