Results 1 to 4 of 4

Thread: remove spaces

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    remove spaces

    Using JavaScript:

    How can I remove spaces from string.

    How can I remove right spaces and left spaces from this string " Hi "

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Trim

    There is not any function in JavaScript to remove spaces such as (Trim function) In VBScript.

    because some browser does not support VBScript so I want to use JavaScript

  4. #4
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    no, trim doesnt exist but you can use this function to emulate it. Put it in the head of the document and call it like this...

    Code:
    newtxt = trim('fk    dns  fnds gkejgij');
    ... and spaces should be removed

    Code:
    function trim(txt){
      var tmp = "";
      var item_length = txt.length;
      var item_length_minus_1 = txt.length - 1;
      for (index = 0; index < item_length; index++){
        if (txt.charAt(index) != ' '){
          tmp += txt.charAt(index);
        }else{
          if (tmp.length > 0){
            if (txt.charAt(index+1) != ' ' && index != item_length_minus_1){
              tmp += txt.charAt(index);
            }
          }
        }
      }
      return tmp;
    }
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

Posting Permissions

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



Click Here to Expand Forum to Full Width