将字符串传递给javascript函数并进行转换

时间:2017-05-12 10:29:09

标签: javascript

var str1 = document.getElementById('string').value;

var str = str1.toUpperCase();

function correctstring() {
    for (var i=0;i < str.length ; i++) {
        if(str[i]==='H'){str[i]='R';}
            else if(str[i]==='V'){str[i]='L';}
            else if (str[i] === 'G'){ str[i]='F'; }
            else  {atert('the string has a wrong input, Please enter the right chars for english(R,L,F) for swedish (H,V,G)');
       }
    }
}

correctstring();

1 个答案:

答案 0 :(得分:0)

字符串在JS中不被视为数组,只需将其转换为数组然后再投射回来:

st = str1.split("");
//Your code
str1 = st.join("")

不要忘记在循环中将 str1 更改为 st