<script> let str = "Hello World!"; // 将字符串的字符全部转换为小写字符 function lowerCase(str) { let arr = str.split(""); let newStr = ""; //通过for循环遍历数组 for (let i = 0; i < arr.length; i++) { if (arr[i] >= 'A' && arr[i] <= 'Z') newStr += arr[i].toLowerCase(); else newStr += arr[i]; } return newStr; } // 将字符串的字符全部转换为大写字符 function upperCase(str) { let arr = str.split(""); let newStr = ""; // 通过数组的forEach方法来遍历数组 arr.forEach(function (value) { if (value >= 'a' && value <= 'z') newStr += value.toUpperCase(); else newStr += value; }); return newStr; } let res1 = lowerCase(str); let res2 = upperCase(str); console.log(res1); console.log(res2); </script>
来源:https://www.cnblogs.com/TomHe789/p/12629342.html