在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP