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
arnololo123Asked:
Who is Participating?
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

0
arnololo123Author Commented:
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

0
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

0
Rainer JeschorCommented:
Hi,

here a working (JavaScript only) sample:
 function CostPOPUP()
{
    var mySplitResult=document.getElementById('HF_LTA').value.split('|');
    for(var i = 0; i < mySplitResult.length; i++)
    {
        if(i==parseInt(document.getElementById('nhRow').value))
		{
            mySplitResult[i] = document.getElementById('Tbx_LTATargetnew').value + '$' + 
                document.getElementById('Tbx_LTAScopenew').value;
            break;
        }
    }
	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:
http://jsfiddle.net/EE_RainerJ/rbyo27jc/

Important:
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.

HTH
Rainer
0

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
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.