1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > JS 统计字符串中大小写字母个数

JS 统计字符串中大小写字母个数

时间:2024-01-25 08:49:53

相关推荐

JS 统计字符串中大小写字母个数

JS 统计字符串中大小写字母个数

注:字母a-z的code为97 - 122,A-Z的code为65 - 90 这很重要。不过记不住也没关系

let str = 'naAZiHesnKuanzgA'console.log('a'.charCodeAt(), 'z'.charCodeAt(), 'A'.charCodeAt(), 'Z'.charCodeAt())//97 122 65 90function countABC(str) {let ABC = [];let abc = [];for (let i in str) {let code = str[i].charCodeAt()if (code > 96 && code < 123) {abc.push(str[i])} else if (code > 64 && code < 91) {ABC.push(str[i])}}console.log(abc.length, ABC.length)}countABC(str)

function countABC(str) {let strArr = Array.from(str)let ABC = [];let abc = [];strArr.forEach(item => {let code = item.charCodeAt()if (code >= 'a'.charCodeAt() && code <= 'z'.charCodeAt()) {abc.push(item)} else if (code >= 'A'.charCodeAt() && code <= 'Z'.charCodeAt()) {ABC.push(item)}})console.log(abc.length, ABC.length)}countABC(str)

两种方法,总之怎么开心怎么来

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。