如何使用 javascript 将任何类型的文本转换为 Toggle Case?
How to convert any type of text into Toggle Case by using javascript?
如何使用 javascript 函数将我的大写、小写、句子大小写转换为切换大小写?任何机构都可以详细说明如何完成吗?
你能看看这个功能对你的案例有帮助吗
function toggleCase(str) {
return str.toUpperCase().split(' ').map(function(word) {
return (word.charAt(0).toLowerCase() + word.slice(1));
}).join(' ');
}
toggleCase("this sentence is in lower case which is to be converted to toggle case");
输出:"tHIS sENTENCE iS iN lOWER cASE wHICH iS tO bE cONVERTED tO tOGGLE cASE"
// For this question, Leading lowercase and remaining uppercase chars
const toggleStr = str =>
str
.split(" ")
.map(word => word[0].toLowerCase() + word.slice(1).toUpperCase())
.join(" ");
// Generic toggle each char based on current char
function toggleChars(str) {
let updated = "";
const [upp_start, upp_end, low_start, low_end] = "AZaz"
.split("")
.map(x => x.charCodeAt(0));
for (let i = 0; i < str.length; i++) {
const charCode = str[i].charCodeAt(0);
let diff = 0;
if (charCode >= upp_start && charCode <= upp_end) {
// change uppercase to lowercase
diff = low_end - upp_end;
} else if (charCode >= low_start && charCode <= low_end) {
// change lowercase to uppercase
diff = upp_end - low_end;
}
updated = updated + String.fromCharCode(charCode + diff);
}
return updated;
}
console.log(toggleStr("stack overflow is great"));
console.log(toggleChars("gReAt sTorY"));
之前的代码工作正常,但在多段的情况下,每段的第一个词不会改变,例如
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
LOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
LOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
LOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
所以我修改了这个问题的代码希望它对其他人有帮助
function togglecase(str){
var str_ar=str.split('\n');
var output='';
$(str_ar).each(function(k,v){
var outText=v.toUpperCase().split(' ').map(function(word) {
return (word.charAt(0).toLowerCase() + word.slice(1));
}).join(' ');
output+=outText+"\n";
});
return output;
}
每个段落的输出都很好并且工作正常
输出:
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
如何使用 javascript 函数将我的大写、小写、句子大小写转换为切换大小写?任何机构都可以详细说明如何完成吗?
你能看看这个功能对你的案例有帮助吗
function toggleCase(str) {
return str.toUpperCase().split(' ').map(function(word) {
return (word.charAt(0).toLowerCase() + word.slice(1));
}).join(' ');
}
toggleCase("this sentence is in lower case which is to be converted to toggle case");
输出:"tHIS sENTENCE iS iN lOWER cASE wHICH iS tO bE cONVERTED tO tOGGLE cASE"
// For this question, Leading lowercase and remaining uppercase chars
const toggleStr = str =>
str
.split(" ")
.map(word => word[0].toLowerCase() + word.slice(1).toUpperCase())
.join(" ");
// Generic toggle each char based on current char
function toggleChars(str) {
let updated = "";
const [upp_start, upp_end, low_start, low_end] = "AZaz"
.split("")
.map(x => x.charCodeAt(0));
for (let i = 0; i < str.length; i++) {
const charCode = str[i].charCodeAt(0);
let diff = 0;
if (charCode >= upp_start && charCode <= upp_end) {
// change uppercase to lowercase
diff = low_end - upp_end;
} else if (charCode >= low_start && charCode <= low_end) {
// change lowercase to uppercase
diff = upp_end - low_end;
}
updated = updated + String.fromCharCode(charCode + diff);
}
return updated;
}
console.log(toggleStr("stack overflow is great"));
console.log(toggleChars("gReAt sTorY"));
之前的代码工作正常,但在多段的情况下,每段的第一个词不会改变,例如
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
LOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
LOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
LOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
所以我修改了这个问题的代码希望它对其他人有帮助
function togglecase(str){
var str_ar=str.split('\n');
var output='';
$(str_ar).each(function(k,v){
var outText=v.toUpperCase().split(' ').map(function(word) {
return (word.charAt(0).toLowerCase() + word.slice(1));
}).join(' ');
output+=outText+"\n";
});
return output;
}
每个段落的输出都很好并且工作正常
输出:
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.
lOREM iPSUM dOLOR sIT aMET, cONSECTETUER aDIPISCING eLIT.