dimanche 30 novembre 2014

foreach char in string


I'm trying to achieve the below Java selection in Javascript, and I'm a little stumped at the moment...


Java:



String input = inputField.getText();

foreach (char c: input.toCharArray){
input = input.replace('a','b');
input = input.replace('b','c');
//and so on...
}
return input;
}


This is what I've tried:



var input = document.getElementById('inputField').value; //input field is textarea

for(var i = 0; i < input.length; i++){
for(input.charAt(i)){
input = input.replace('a','b');
input = input.replace('b','c');
}
}
return input;
}


I've also tried some different variations, such as disregarding the loop entirely and just using replace; however, then the replaces will just fire off sequentially and will not cover the length of long multi spaced strings.


Any help on this would be greatly appreciated. Thanks!





Aucun commentaire:

Enregistrer un commentaire