Update a string seperated by | using split


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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
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

arnololo123Author Commented:

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++)
          {mySplitResult[i]=document.all('<%= Tbx_LTATargetnew.ClientID%>').value +'$'+document.all('<%= Tbx_LTAScopenew.ClientID%>').value;}
       alert( document.all('<%=HF_LTA.ClientID%>').value.split('|')[document.all('<%=inhRow.ClientID%>').value].split('$')[0]);

Open in new window

Jeff DarlingDeveloper AnalystCommented:
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

Rainer JeschorCommented:

here a working (JavaScript only) sample:
 function CostPOPUP()
    var mySplitResult=document.getElementById('HF_LTA').value.split('|');
    for(var i = 0; i < mySplitResult.length; i++)
            mySplitResult[i] = document.getElementById('Tbx_LTATargetnew').value + '$' + 
	document.getElementById('HF_LTA').value = mySplitResult.join('|');
	alert( document.getElementById('HF_LTA').value.split('|')[document.getElementById('nhRow').value].split('$')[0]);

Open in new window

Live Version:

You should not use document.all but document.getElementById instead as the latter one is compatible with all Browsers.

And you can pass the join function an additional parameter to set the join character.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnololo123Author Commented:
Great answer with an example
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.