Results 1 to 20 of 20

Thread: javascript trim() equivalent?

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    javascript trim() equivalent?

    as the title - what is the equivalent of the trim() statement from vb in javascript please ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Arrow hi

    I dont think there is any !!!

    But this would work:



    function trimspaces(word12)
    {
    word12 = word12 + " ";
    var returnword = "";
    len1 = word12.length;

    flag = 0;
    for(ctr = 0 ; ctr<len1;ctr++)
    {
    mchr = word12.charAt(ctr);
    if(mchr != " " || flag == 1 )
    {
    returnword = returnword + mchr;
    flag = 1;
    }

    }

    var newword = "";
    word12 = returnword;
    len1 = word12.length;
    flag = 0;
    for(ctr = len1-1 ; ctr >= 0 ;ctr--)
    {
    mchr = word12.charAt(ctr);
    if(mchr != " " || flag == 1 )
    {
    newword = mchr + newword ;
    flag = 1;
    }

    }


    word12 = newword;
    newword = ""
    len1 = word12.length;
    var mchr1 = "";
    for(ctr = 0 ; ctr <len1 ;ctr++)
    {
    mchr = word12.charAt(ctr);
    if(mchr != " " || mchr1 != " " )
    {
    newword = newword + mchr;
    }
    mchr1=mchr;

    }



    return(newword);

    }

    Usage :

    word = trimspaces(mvalue);

  3. #3

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Thanks for that John - I'm gonna have to ring MS about your other Q also

    As for this one, thanks, but I have used one before (there is an equivalent) I just can't think of it at the moment

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    kayoca
    Guest
    Perhaps you can use this code for it. Just replacing all the spaces with nothing.
    Code:
    var sString = 'Hallo ik ben Gek';
    var sResult = '';
    var sReplace = / /g;
    
    // removing all the spaces
    sResult = sString.replace(sReplace,'');

  5. #5
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    Tell me also when you find it

  6. #6
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    But if you just want to replace the leading and trailing whitespace...

    Code:
    var myString = "  I have Mojo Foo  ";
    
    // trim myString
    myString.replace(/^\s+|\s+$/g, "");
    Wow... that was simple.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  7. #7

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    /^\s+|\s+$/g
    This works great thanks !!!!

    Can someonbe explain these parts though - I knowwhat the replace bit does, but can't find any of hese in my books, what do each parts of this statement mean please ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Sure, not a problem, though there is an O'Reilly RegEx book that might help, the Rhino (JavaScript) book, which has these listed, and the best: The Llama (Perl) book.

    The two /'s are meant to quote a regular expression. You should be able to use any character for this, as long as you use the same character in both places, and don't confuse the parser by using the same character in the expression. If you do you will have to escape it. Avoiding escaping / is why you would want to use something other than /, but now I digress.

    The trailing g means greedy. It tells the expression to match as many times as possible.

    Okay, inside the expression we have two seperate expressions, joined by a union (or). The | is the logical or operator.

    The expression on the left means "whitespace that occurs one or more times at the start of the expression". The \s is the escape sequence for white space characters (space or tab). The + is a modifier. It means "one or more times". The ^ is an operator that marks the beginning of the expression.

    On the right side we see the \s+ again, which means "whitespace characters that appear one or more times". But instead of the caret marking the begining of the expression, we have the $ marking the end of the expression. The right side now means "whitespace that occurs one or more times at the end of the expression".

    Note: /\s+$|^\s+/g would be the same thing. Don't assume you have to put the clause that must match the beginning of the expression first. That is just done for readability, the or clause doesn't care who is when. Mind however, that if you don't use the greedy flag, then these two expression could be different since your expression will only match once for the expression (either at the beginning or the end of the expression depending on the order the or is evaluated in).

    This is case sensitive. \S actually means any non-whitespace character.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  9. #9

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Thanks for taking the time to explain that - helped me a lot (& I'll get that book )

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Arrow hi

    Though that is not a function, but a great line of code!!!!

    So tell me how to use it.

  11. #11
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    What is not a function, and what do you need help with?

    Being ignorant is one thing, but being vague in your ignorance is just plain stupid.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  12. #12
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    I tried this code and it did NOT remove the spaces
    Code:
    var strTestStringForSpaces = document.form.txtOther9.value
    	//trim
    	strTestStringForSpaces.replace(/^\s+|\s+$/g, "")
    	alert(strTestStringForSpaces.replace(/^\s+|\s+$/g, ""))

  13. #13
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    Originally posted by CiberTHuG
    What is not a function, and what do you need help with?

    Being ignorant is one thing, but being vague in your ignorance is just plain stupid.
    I so don't understand that quote. I have no idea what I was saying there.

    J4U, it did work. But you are trying to print out the return value from the replace operation, not the value of the string after the replace.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  14. #14
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Travis G
    I so don't understand that quote. I have no idea what I was saying there.

    J4U, it did work. But you are trying to print out the return value from the replace operation, not the value of the string after the replace.
    I tried both ways

  15. #15
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    Sorry, I was thinking the replace method would change the value of the string. You were right to call the return of the replace, not the string after the replace.

    Odd though, but this code works.

    Code:
    <html>
      <head>
        <title>Obvious Factor</title>
      </head>
      <body>
        <script type="text/javascript">
          var strTestStringForSpaces = "  Foo Bar Blah  ";
          alert("|" + strTestStringForSpaces + "|");
          alert("|" + strTestStringForSpaces.replace(/^\s+|\s+$/g, "") + "|");
        </script>
        <p>Done.</p>
      </body>
    </html>
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  16. #16
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    thanks but I am trying it in this scenario and it doesn't execute
    Code:
    function textarea1_onblur() {
    	//clears the checkbox when there is just a space in there.
    	var strTestStringForSpaces = document.form.textarea1.value
    	
    	
    	strTestStringForSpaces.replace(/^\s+|\s+$/g, "") 
    	
    	if (strTestStringForSpaces == ""){
    		document.form.textarea1.value = ""
    		document.form.chkOther.checked = false
    	}
    }

  17. #17
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    got it
    Code:
    function textarea1_onblur() {
    	//clears the checkbox when there is just a space in there.
    	var strTestStringForSpaces = document.form.textarea1.value
    	
    	
    	strTestStringForSpaces = strTestStringForSpaces.replace(/^\s+|\s+$/g, "") 
    	
    	if (strTestStringForSpaces == ""){
    		document.form.textarea1.value = ""
    		document.form.chkOther.checked = false
    	}
    }

  18. #18
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    You could do this:

    Code:
    function textarea1_onblur() {
    	//clears the checkbox when there is just a space in there.
    	var strTestStringForSpaces = document.form.textarea1.value
    	
    	if (strTestStringForSpaces.search(/\S/) != -1){
    		document.form.textarea1.value = ""
    		document.form.chkOther.checked = false
    	}
    }
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  19. #19
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    sorry didn't work

  20. #20
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    Sorry. This is what I used to test it. Perhaps I did something wrong when I adapted it to your function.

    Code:
    <html>
      <head>
        <title>Obvious Factor</title>
      </head>
      <body>
        <script type="text/javascript">
          var strTestStringForSpaces = "    ";
          alert("|" + strTestStringForSpaces + "|");
          if(strTestStringForSpaces.search(/\S/) != -1) {
            alert("Non-empty string");
          }
        </script>
        <p>Done.</p>
      </body>
    </html>
    Now that I look at it, maybe you want == in the if, not !=. Sorry. I'm not exactly sure what you are trying to do with your stuff. I wasn't paying that much attention to the periphery.

    I know it isn't a browser issue, though. I tested it on the world's worst browser, so I'm sure all others can handle search().
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

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