在JS中手动将大写字符串转换为小写字符串
Convert uppercase string to lowercase string in JS manually
尝试编写一个函数,将 returns 调用字符串值转换为
小写。诀窍是我不能使用 toLocaleLowerCase()
.
以下是我目前的资料。
function charChange(char){
for (var i=0; i<char.length; i++){
var char2=charCodeAt(char[i])+32;
var char3=String.fromCharCode(char2);
if (char3 !== charCodeAt(97-122){
alert("Please enter only letters in the function")
}
}
return char;
}
您可以使用regex将字符串转换为小写:
:%s/.*/\L&/
charCodeAt()
is a method called on a String, it's not a function. So you apply the method on a string and give the position of the character you want to convert as the parameter. Also as MultiplyByZer0 mentioned the word char
is reserved: look at the list of reserved words.
以下代码修复了问题:
function charChange(str) {
var result = "";
for (var i = 0; i < str.length; i++) {
var code = str[i].charCodeAt(0);
if(code >= 65 && code <= 90) {
var letter = String.fromCharCode(code+32);
result += letter // append the modified character
} else {
result += str[i] // append the original character
}
}
return result;
}
console.log(charChange('j#aMIE'));
要手动将字符串的大写字母转换为小写字母(如果您不是手动进行,您应该只使用String.prototype.toLowerCase()
),您必须:
编写样板函数内容:
function strLowerCase(str) {
let newStr = "";
// TODO
return newStr;
}
循环遍历原始字符串的每个字符,并获取其代码点:
function strLowerCase(str) {
let newStr = "";
for(let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
// TODO
} return newStr;
}
检查字符是否为大写字母。 In ASCII,代码点在65
和90
之间(含)的字符为大写字母。
function strLowerCase(str) {
let newStr = "";
for(let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
if(code >= 65 && code <= 90) {
// TODO
} // TODO
} return newStr;
}
如果字符是大写,则在其代码点上添加 32 使其变为小写(是的,这是 ASCII 的创建者有意设计的决定)。无论如何,将新字符附加到新字符串。
function strLowerCase(str) {
let newStr = "";
for(let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
if(code >= 65 && code <= 90) {
code += 32;
} newStr += String.fromCharCode(code);
} return newStr;
}
测试你的新功能:
strLowerCase("AAAAAAABBBBBBBCCCCCZZZZZZZZZaaaaaaaaaaa&$*(@&(*&*#@!");
// "aaaaaaabbbbbbbccccczzzzzzzzzaaaaaaaaaaa&$*(@&(*&*#@!"
这是我(目前)能想到的两个最优雅的解决方案。查看代码中的注释,如果有任何不清楚的地方,请随时询问。
function charChange1 (str) {
let result = '';
const len = str.length;
// go over each char in input string...
for (let i = 0; i < len; i++) {
const c = str[i];
const charCode = c.charCodeAt(0);
if (charCode < 65 || charCode > 90) {
// if it is not a uppercase letter,
// just append it to the output
// (also catches special characters and numbers)
result += c;
} else {
// else, transform to lowercase first
result += String.fromCharCode(charCode - 65 + 97);
}
}
return result;
}
function charChange2 (str) {
// Array.prototype.slice.call(str) converts
// an array-like (the string) into an actual array
// then, map each entry using the same logic as above
return Array.prototype.slice.call(str)
.map(function (c) {
const charCode = c.charCodeAt(0);
if (charCode < 65 || charCode > 90) return c;
return String.fromCharCode(charCode - 65 + 97);
})
// finally, join the array to a string
.join('');
}
console.log(charChange1("AAAAsfasSGSGSGSG'jlj89345"));
console.log(charChange2("AAAAsfasSGSGSGSG'jlj89345"));
(在侧节点上,当然可以用声明为调用 'A'.charCodeAt(0)
的常量替换幻数)
(第二个节点:不要使用char因为它是JavaScript中的保留字;我更喜欢c)
尝试编写一个函数,将 returns 调用字符串值转换为
小写。诀窍是我不能使用 toLocaleLowerCase()
.
以下是我目前的资料。
function charChange(char){
for (var i=0; i<char.length; i++){
var char2=charCodeAt(char[i])+32;
var char3=String.fromCharCode(char2);
if (char3 !== charCodeAt(97-122){
alert("Please enter only letters in the function")
}
}
return char;
}
您可以使用regex将字符串转换为小写:
:%s/.*/\L&/
charCodeAt()
is a method called on a String, it's not a function. So you apply the method on a string and give the position of the character you want to convert as the parameter. Also as MultiplyByZer0 mentioned the word char
is reserved: look at the list of reserved words.
以下代码修复了问题:
function charChange(str) {
var result = "";
for (var i = 0; i < str.length; i++) {
var code = str[i].charCodeAt(0);
if(code >= 65 && code <= 90) {
var letter = String.fromCharCode(code+32);
result += letter // append the modified character
} else {
result += str[i] // append the original character
}
}
return result;
}
console.log(charChange('j#aMIE'));
要手动将字符串的大写字母转换为小写字母(如果您不是手动进行,您应该只使用String.prototype.toLowerCase()
),您必须:
编写样板函数内容:
function strLowerCase(str) { let newStr = ""; // TODO return newStr; }
循环遍历原始字符串的每个字符,并获取其代码点:
function strLowerCase(str) { let newStr = ""; for(let i = 0; i < str.length; i++) { let code = str.charCodeAt(i); // TODO } return newStr; }
检查字符是否为大写字母。 In ASCII,代码点在
65
和90
之间(含)的字符为大写字母。function strLowerCase(str) { let newStr = ""; for(let i = 0; i < str.length; i++) { let code = str.charCodeAt(i); if(code >= 65 && code <= 90) { // TODO } // TODO } return newStr; }
如果字符是大写,则在其代码点上添加 32 使其变为小写(是的,这是 ASCII 的创建者有意设计的决定)。无论如何,将新字符附加到新字符串。
function strLowerCase(str) { let newStr = ""; for(let i = 0; i < str.length; i++) { let code = str.charCodeAt(i); if(code >= 65 && code <= 90) { code += 32; } newStr += String.fromCharCode(code); } return newStr; }
测试你的新功能:
strLowerCase("AAAAAAABBBBBBBCCCCCZZZZZZZZZaaaaaaaaaaa&$*(@&(*&*#@!"); // "aaaaaaabbbbbbbccccczzzzzzzzzaaaaaaaaaaa&$*(@&(*&*#@!"
这是我(目前)能想到的两个最优雅的解决方案。查看代码中的注释,如果有任何不清楚的地方,请随时询问。
function charChange1 (str) {
let result = '';
const len = str.length;
// go over each char in input string...
for (let i = 0; i < len; i++) {
const c = str[i];
const charCode = c.charCodeAt(0);
if (charCode < 65 || charCode > 90) {
// if it is not a uppercase letter,
// just append it to the output
// (also catches special characters and numbers)
result += c;
} else {
// else, transform to lowercase first
result += String.fromCharCode(charCode - 65 + 97);
}
}
return result;
}
function charChange2 (str) {
// Array.prototype.slice.call(str) converts
// an array-like (the string) into an actual array
// then, map each entry using the same logic as above
return Array.prototype.slice.call(str)
.map(function (c) {
const charCode = c.charCodeAt(0);
if (charCode < 65 || charCode > 90) return c;
return String.fromCharCode(charCode - 65 + 97);
})
// finally, join the array to a string
.join('');
}
console.log(charChange1("AAAAsfasSGSGSGSG'jlj89345"));
console.log(charChange2("AAAAsfasSGSGSGSG'jlj89345"));
(在侧节点上,当然可以用声明为调用 'A'.charCodeAt(0)
的常量替换幻数)
(第二个节点:不要使用char因为它是JavaScript中的保留字;我更喜欢c)