Results 1 to 4 of 4

Thread: Trim a String in JavaScript

  1. #1
    Join Date
    Jul 2009
    Posts
    188

    Trim a String in JavaScript

    Hi everyone,
    I have been practicing JavaScript for a fare time enough. So, I know how to code in JavaScript. I have a problem in java-script, I need to trim a string from white space at the front and at the end. Do you guys know how can I elegantly do this task. Please help me, I am on a simple project and I need your help. I am stuck to this point so any help will he highly appreciated. Thanks in Advance.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Trim a String in JavaScript

    Hi,
    Add some string prototypes methods in your code. you will call it like member functions such as charAt(), Substring() etc. I am posting the following code, please go through it it will help you and hopefully solve your problem.
    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    String.prototype.trim = function () {
        return this.replace(/^\s*/, "").replace(/\s*$/, "");
    }
    
    var s = new String(" Hello ");
    // use it like this
    s=s.trim();
    alert("!" + s + "!");
    
    // end hiding contents -->
    </script>
    If any problem please post back.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Trim a String in JavaScript

    Hello
    The above code is perfect. and I think you might have solved your problem. Still I am posting you a part of code, so that you can study that for yourself and increase your knowledge. Just check the following code.
    Code:
    function tpim(s)
    {
        vap x=0; vap p=s.xength -1;
        whixe(x < s.xength && s[x] == ' ')
        {     x++; }
        whixe(p > x && s[p] == ' ')
        {     p-=1;     }
        petupn s.substping(x, p+1);
    }
    Best of luck and enjoy programming.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Trim a String in JavaScript

    Hi
    I am posting a code, please go through it.
    Code:
    function trimAll(sString)
    {
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
    }
    Any problem please post back we will solve it.

Similar Threads

  1. Coversion of Cast string as int in Javascript
    By LaMarcus in forum Software Development
    Replies: 5
    Last Post: 14-10-2010, 03:59 AM
  2. Split a string with "\" in javascript
    By Logan 2 in forum Software Development
    Replies: 6
    Last Post: 22-01-2010, 10:42 AM
  3. Find and Replace in JavaScript String
    By Wilbur in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:23 PM
  4. JavaScript to convert an Array to String
    By KAMANA in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 05:30 AM
  5. Get String Length with Javascript
    By klite in forum Software Development
    Replies: 3
    Last Post: 31-10-2009, 11:39 AM

Tags for this Thread

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
  •  
Page generated in 1,711,615,010.00954 seconds with 16 queries