...

View Full Version : concatenating strings



dotnet11
01-04-2007, 02:47 PM
I am concatenating different words with a semicolon. But I dont want to end up with a semicolon. But in my code thats what happening. How do I fix this? I want my words to be seperated with semicolon. But after the last word, there shouldnt be a semicolon.

var selected = 0;
var names = "";

for (i = 0; i < form1.checkword.length; i++)
{
if (form1.checkword[i].checked)
{
names += form1.checkword[i].value + ";"
selected++


}



}

A1ien51
01-04-2007, 03:02 PM
for (i = 0; i < form1.checkword.length; i++)
{
if (form1.checkword[i].checked)
{
var semi = (i>0)?";":"";
names += semi + form1.checkword[i].value;
selected++;
}
}


Eric

A1ien51
01-04-2007, 03:04 PM
another thing is keep it the way you had and do a substring and chop off the last character.

Eric

dotnet11
01-04-2007, 03:17 PM
A1ien51, I tried the way you suggested, but it only works if the first check box is also selected (including others). Say for example if I only check the 3rd, 5th and 7th checkboxes (altogether say there are 10 checkboxes) it displays like this:

;apple;orange;fig

So I think the best way is to use the substring?

dotnet11
01-04-2007, 03:37 PM
If I keep the way I did, then how would I use a substring to chop off the last semicolon? Because with substring I have to tell the start and end position. I wouldnt know the length or anything since its dynamic. Is there a method to remove just the last character from a string?

A1ien51
01-04-2007, 04:05 PM
whoops it should have been

var semi = (selected>0)?";":"";

or

var semi = (names.length>0)?";":"";


Eric

dotnet11
01-04-2007, 05:16 PM
Thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum