locked
Javascript - string values into array issue RRS feed

  • Question

  • User1010413649 posted

    This code outputs: ;test1; test2; test3

    I would like it as test1; test2; test3

    var str = "test1, test2, test3"
    var list = str.split(",");
    var arr = new Array();
    var seperator = ";";
    for(var x in list)
    {
      arr += ";" + list[x]
      alert(arr);
    }

    Any suggestion?

    Monday, October 9, 2017 10:31 PM

Answers

  • User-474980206 posted
    // example 1
    var str = "test1, test2, test3"
    var arr = str.split(",").join(";");
    
    alert(arr);
    
    // example 2
    
    var str = "test1, test2, test3"
    var arr = str.replace(",",";")
    
    alert(arr);
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 10, 2017 1:38 AM

All replies

  • User-474980206 posted
    // example 1
    var str = "test1, test2, test3"
    var arr = str.split(",").join(";");
    
    alert(arr);
    
    // example 2
    
    var str = "test1, test2, test3"
    var arr = str.replace(",",";")
    
    alert(arr);
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 10, 2017 1:38 AM
  • User1010413649 posted

    Thanks

    Tuesday, October 10, 2017 1:56 PM