Thursday, August 31, 2017

Split and join in JavaScript


Split a phrase into words
This function takes a given phrase and splits based on space into a new array
ex : It would take "Prathap  Kudupu"  it would add it to an array with prathap and kudupu

Edit in plnkr
 console.clear();
  console.log("**** Split a phrase *****");
  
  function splitPhrase(str){
    console.log(str);
    return str.split(' ');
  }
  console.log(splitPhrase('Prathap Kudupu'));
  



Join a phrase into words
 The join function would take array of strings as input and then join elements into a phrase

console.log("\n****Join a Phrase *****");
   function joinPhrase(str){
    //Get array of strings
   
    str=splitPhrase(str);
    console.log(str);
    return str.join(' ');
  }

Output

Split and join characters.

To split and join a character we need can use the same function however we need to use split with

return str.split('');




No comments:

Post a Comment