Want to get rid of Google Ads, click here.
+ Reply to Thread
Results 1 to 3 of 3

Thread: SCRIPT: Remove Leading and Trailing Spaces from String

  1. #1
    Member
    Join Date
    Feb 2002
    Location
    Phoenix, AZ
    Posts
    34

    Default SCRIPT: Remove Leading and Trailing Spaces from String

    Here's a function to remove spaces from beginning and end of a string. Not much to it...hope you like it.

    Code:
    function Trim( strString ) {
        //Removes leading and trailing spaces from a string.
    
        try {
            //If parameter was null, return an empty string.
            if (strString==null) {
                return "";
            }
            
            var strNewString = "";
            var c = 0;
            for (i=0; i<strString.length; i++) {
                if (strString.charAt(i) != " " || c > 0) {
                    strNewString += strString.charAt(i);
                    if (strString.charAt(i) != " ") c = strNewString.length;
                }
            }
            return strNewString.substr(0,c);
        }
        catch(e) {
            //your error handler here
        }
        
    }

  2. #2
    Senior Member glg's Avatar
    Join Date
    Aug 2004
    Location
    Chicago, IL, USA
    Posts
    715

    Default

    For some of this stuff, remember that you can use the system language stuff in Javascript.

    system.functions.strclpl(string)
    system.functions.strclpr(string)

  3. #3
    Junior Member
    Join Date
    Mar 2006
    Location
    Stavanger, Norway
    Posts
    10

    Default

    Code:
    strString.replace(/^ +(.*?) +$/, "$1")
    does the same thing, I believe.




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

Posting Permissions

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