當前位置:
首頁 >
字符串大小写互换方法
發布時間:2025/3/21
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
字符串大小写互换方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 String.prototype.toAlternatingCase = function () {
2 return this.split("").map(a => a === a.toUpperCase() ?
3 a.toLowerCase(): a.toUpperCase()).join('')
4 }
該方法中判斷當前字母是大寫字母還是小寫字母的方法是:?
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()對非字母的字符不產生作用,所以判斷的時候不需要char >= 'a'? && char <= 'z'
? 2) 不能連寫? ''a'<=char<='z',而應該寫成char >= 'a'? && char <= 'z'?
轉載于:https://www.cnblogs.com/lyraLee/p/10046073.html
總結
以上是生活随笔為你收集整理的字符串大小写互换方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这个窗体的制作
- 下一篇: How to solve random