Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    concatenating strings

    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++


    }



    }

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Code:
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    another thing is keep it the way you had and do a substring and chop off the last character.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by dotnet11; 01-04-2007 at 04:35 PM.

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    whoops it should have been

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

    or

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


    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •