Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中将字符串中的奇数和偶数索引字符转换为大写/小写?_Javascript_String_Uppercase_Lowercase - Fatal编程技术网

在Javascript中将字符串中的奇数和偶数索引字符转换为大写/小写?

在Javascript中将字符串中的奇数和偶数索引字符转换为大写/小写?,javascript,string,uppercase,lowercase,Javascript,String,Uppercase,Lowercase,我需要创建一个函数,用于读取字符串输入,并将字符串中的奇数索引字符转换为大写,将偶数索引字符转换为小写 function alternativeCase(string){ for(var i = 0; i < string.length; i++){ if (i % 2 != 0) { string[i].toUpperCase(); } else { string[i].toLower

我需要创建一个函数,用于读取字符串输入,并将字符串中的奇数索引字符转换为大写,将偶数索引字符转换为小写

function alternativeCase(string){
    for(var i = 0; i < string.length; i++){
        if (i % 2 != 0) {
            string[i].toUpperCase();
        }
        else {
            string[i].toLowerCase();
        }   
    }
    return string;
}
function alternativeCase(字符串){
对于(变量i=0;i

如何修复我的代码?

JavaScript中的字符串是不可变的,请尝试以下方法:

function alternativeCase(字符串){
var newString=[];
对于(变量i=0;i}
JavaScript中的字符串是不可变的,请尝试以下方法:

function alternativeCase(字符串){
var newString=[];
对于(变量i=0;i
试试这个:

function alternativeCase(string){
    var output = "";
    for(var i = 0; i < string.length; i++){
        if (i % 2 != 0) {
            output += string[i].toUpperCase();
        }
        else {
            output += string[i].toLowerCase();
         }   
    }
    return output;
}
function alternativeCase(字符串){
var输出=”;
对于(变量i=0;i
试试这个:

function alternativeCase(string){
    var output = "";
    for(var i = 0; i < string.length; i++){
        if (i % 2 != 0) {
            output += string[i].toUpperCase();
        }
        else {
            output += string[i].toLowerCase();
         }   
    }
    return output;
}
function alternativeCase(字符串){
var输出=”;
对于(变量i=0;i

2019年更新 现在使用ES6语法非常安全:

const alternativeCase=string=>string.split(“”)
.map((c,i)=>i&1?c.toUpperCase():c.toLowerCase()).join(“”);

2019年更新 现在使用ES6语法非常安全:

const alternativeCase=string=>string.split(“”)
.map((c,i)=>i&1?c.toUpperCase():c.toLowerCase()).join(“”);

处理字符之间空格的RegExp替代方案:

const alternativeCase=s=>s.replace(/(\s\s*)(\s?)/g,(m,a,b)=>a.toUpperCase()+b.toLowerCase());

console.log(alternativeCase('alternativeCase'))
RegExp alternative,用于处理字符之间的空格:

const alternativeCase=s=>s.replace(/(\s\s*)(\s?)/g,(m,a,b)=>a.toUpperCase()+b.toLowerCase());

console.log(alternativeCase('alternativeCase'))
可能重复的可能重复的可能重复的谢谢!这对我非常有效!我的代码离我不远!不,你离得不远,你离得很近!正如其他注释所提到的,JS中的字符串是“不可变的”,这意味着如果不将其分配给新变量(或其本身),就无法在内存中对其进行更改,这就是为什么不能按原来的方式在字符串中设置字符。这是你的方法唯一的问题。谢谢!这对我非常有效!我的代码离我不远!不,你离得不远,你离得很近!正如其他注释所提到的,JS中的字符串是“不可变的”,这意味着如果不将其分配给新变量(或其本身),就无法在内存中对其进行更改,这就是为什么不能按原来的方式在字符串中设置字符。这是你的方法唯一的问题。