Link to home
Start Free TrialLog in
Avatar of arnololo123
arnololo123

asked on

Update a string seperated by | using split

Hi,

I have  a string, let's say:  var mystring=  '6|8|444|2|9|4|2|1|6|77|998|111'

I would like to change in JAvscript for example the  value 444 in the string by another value assuming that I know that 444 is at position 3

Thanks
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

var mystring=  '6|8|444|2|9|4|2|1|6|77|998|111'
var arr = mystring.split("|");
//replace value
var result = arr.join();

Open in new window

Avatar of arnololo123
arnololo123

ASKER

Hello,

I can't make it work, please see below my function:
 function CostPOPUP()
        {
        var mySplitResult=document.all('<%=HF_LTA.ClientID%>').value.split('|');
        for(i = 0; i < mySplitResult.length; i++)
          {
          if(i==parseInt(document.all('<%=inhRow.ClientID%>').value))
          {mySplitResult[i]=document.all('<%= Tbx_LTATargetnew.ClientID%>').value +'$'+document.all('<%= Tbx_LTAScopenew.ClientID%>').value;}
         
        }
        document.all('<%=HF_LTA.ClientID%>').value=mySplitResult.join();
       alert( document.all('<%=HF_LTA.ClientID%>').value.split('|')[document.all('<%=inhRow.ClientID%>').value].split('$')[0]);
}

Open in new window

Avatar of Jeff Darling
This changes the 444 to 555.  Also puts the pipe characters back after the array join.

var strValues ='6|8|444|2|9|4|2|1|6|77|998|111';
var arrValues = strValues.split('|');
arrValues[2] = 555;
var strOut = arrValues.join();
strOut = strOut.replace(/,/g,"|");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great answer with an example