在 JavaScript 中将混合大小写字符串转换为小写
问题
我们需要编写一个JavaScript函数convertToLower(),该函数接受一个字符串方法,该方法将调用它的字符串转换为小写字符串并返回新字符串。
例如,如果函数的输入是
输入
const str = 'ABcD123';
输出
const output = 'abcd123';
示例
以下是代码-
const str = 'ABcD123';
String.prototype.convertToLower = function(){
let res = '';
for(let i = 0; i < this.length; i++){
const el = this[i];
const code = el.charCodeAt(0);
if(code >= 65 && code <= 90){
res += String.fromCharCode(code + 32);
}else{
res += el;
};
};
return res;
};
console.log(str.convertToLower());输出结果abcd123
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短