在 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 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短