Another JavaScript function for trim a string from white space

fonts-48x48 Thanks Joules  left a comment to let me konw that the language attribute for the script tag is deprecated (the article trim a string from white space by JavaScript function ).I wrote another javascript function for trim a string from white space at the front.(It will need some change for RTrim and Trim),I don’t konw there is faster way than below,if you know tell me pls. :)

function LTrim(str) {
   var i;
   var len = str.length;

  for( i=0; i<len; i++ )
   {
      if( str.charCodeAt(i) == 160 )
      {
         str = str.substring(1, str.length );
         i--;
         len--;
      }
      else
      {
         break;
      }
   }
   return str;
 }

comment:160 is the char code for white space.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

7 Responses to “Another JavaScript function for trim a string from white space”

  1. Nate says:

    Try this…

    function trim(value) {
    return value.replace(/^\s+|\s+$/g, ”);
    }

  2. minidxer says:

    hey,Nate
    Thanks for your function.I tried it at IE6.0 and FireFox2.0
    The result is:
    FF2 is OK but can not be used with IE6…
    There was no error but the white space can not be trimmed

  3. [...] This extension splits the string by given separator and returns an array with trimmed  items. It uses the trim() extension at trim a string from white space by JavaScript function or Another JavaScript function for trim a string from white space. [...]

  4. Andrew says:

    /**
    * Returns a copy of string without leading and trailing whitespace.
    * Whitespace is defined as {char c | c <= ‘ ‘}.
    *
    * Note: We avoid regular expressions in implementation to support older user agents.
    */
    function trim(string) {

    /* Lookup the LENGTH of string. */
    var LENGTH = string.length;

    /* Find the first character that is not whitespace. */
    var beginIndex = 0;
    while (beginIndex != LENGTH && string.charAt(beginIndex) <= ‘ ‘) {
    beginIndex++;
    }

    /* If all characters are whitespace then return the empty String. */
    if (beginIndex == LENGTH) {return “”;}

    /* Find the last character that is not whitespace. */
    var endIndex = LENGTH;
    while (endIndex != beginIndex && string.charAt(endIndex – 1) <= ‘ ‘) {
    endIndex–;
    }

    /*
    * If no leading or trailing whitespace found then return the string,
    * otherwise return the substring without leading and trailing whitespace.
    */
    return ((beginIndex == 0) && (endIndex == LENGTH)) ?
    string : string.substring(beginIndex, endIndex);
    }

  5. Andrew says:

    Your blog software messed up the code. The second while loop uses the postfix decrement operator, and the quotes should be either the single ASCII quote or the double ASCII quote.

  6. [...] a previous post trim a string from white space by JavaScript function ( or Another JavaScript function for trim a string from white space ) and JavaScript function-Splits the string by given separator and returns an array with trimmed [...]

  7. Gaurav Singh says:

    Dear Sir
    I want to Know how can i find Space in String in VB Script ASP

Leave a Reply