字符串大小写互换方法
生活随笔
收集整理的這篇文章主要介紹了
字符串大小写互换方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 String.prototype.toAlternatingCase = function () {
2 return this.split("").map(a => a === a.toUpperCase() ?
3 a.toLowerCase(): a.toUpperCase()).join('')
4 }
該方法中判斷當(dāng)前字母是大寫字母還是小寫字母的方法是:?
a === a.toUpperCase() 或者 a === a.toLowerCase()
String.prototype.toAlternatingCase = function () { return this.replace(/[a-zA-Z]/g, (i) => i>='a' ? i.toUpperCase():i.toLowerCase()) }上面的方法中判斷大小寫的方法是:
? ? ?在搜查出的大小寫字母中,char >= 'a' 是小寫字母;反之值大寫字母。
? ? ?注意: 1)toUpperCase(),toLowerCase()對(duì)非字母的字符不產(chǎn)生作用,所以判斷的時(shí)候不需要char >= 'a'? && char <= 'z'
? 2) 不能連寫? ''a'<=char<='z',而應(yīng)該寫成char >= 'a'? && char <= 'z'?
轉(zhuǎn)載于:https://www.cnblogs.com/lyraLee/p/10046073.html
總結(jié)
以上是生活随笔為你收集整理的字符串大小写互换方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这个窗体的制作
- 下一篇: How to solve random