Answers for "string foreach"

2

java foreach char

for (char ch: "xyz".toCharArray()) {
  System.out.println(ch); 
}
Posted by: Guest on August-21-2020
0

string foreach javascript

//for
  str='hello'
  for (var i = 0; i < str.length; i++) {
    console.info(str[i]);
  }

//for...of
  let result1 = '';
  for (let letter of str) {
    result1 += letter;
  }
  console.log(`for...of : ${result1}`)

//forEach

  // ES6 version.
    let result2 = '';
    str.split('').forEach(letter => {
      result2 += letter;
    });
    console.log(`forEach ES6 : ${result2}`)

  //or
    var result3 = '';
    str.split('').forEach(function(letter) {
      result3 += letter;
    });
    console.log(`forEach standard : ${result3}`)

//for...in
  var result4 = '';
  for (var letterIndex in str) {
    result4 += str[letterIndex];
  }
    console.log(`for...in : ${result4}`)
//map
	// ES6 version.
      var result5 = '';
      str.split('').map(letter => {
        result5 += letter;
      });
      console.log(`map ES6 : ${result5}`)
	
	//or
      let result6 = '';
      str.split('').map(function(letter) {
        result6 += letter;
      });
      console.log(`map standard : ${result6}`)
Posted by: Guest on August-24-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language