Wrox Programmer Forums
|
Classic ASP Components Discussions specific to components in ASP 3.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Components section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 31st, 2003, 05:03 PM
Registered User
 
Join Date: Sep 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Trim a value in Java Script ?

Hi all ,
I am a vb progrmmer but recently switched to Web develpment.I want to know how to trim a value in a string using java script if there is any such function available like Vb.
Thanks,
Misbah H. Ansari.

 
Old October 31st, 2003, 05:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Below you'll find three methods: LTrim, RTrim and Trim. The code in these methods is a bit out-dated )regular expressions would make more sense), but they do work.
Code:
// Trims all spaces to the left of a specific string
function LTrim(str)
{
        var whitespace = new String(" \t\n\r "); 
        // last space character is not a space, but alt+0160, 
        // another invisible char. 
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(0)) != -1) {
            // We have a string with leading blank(s)...
            var j=0, i = s.length;
            // Iterate from the far left of string until we
            // don't have any more whitespace...
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;
            // Get the substring from the first non-whitespace
            // character to the end of the string...
            s = s.substring(j, i);
        }
        return s;
}
// Trims all spaces to the right of a specific string
function RTrim(str)
{
        // We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in whitespace
        var whitespace = new String(" \t\n\r "); 
        // last space character is not a space, but alt+0160,
        // another invisible char. 
        var s = new String(str);
        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            // We have a string with trailing blank(s)...
            var i = s.length - 1;       // Get length of string
            // Iterate from the far right of string until we
            // don't have any more whitespace...
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;
            // Get the substring from the front of the string to
            // where the last non-whitespace character is...
            s = s.substring(0, i+1);
        }
        return s;
}

// Trims all spaces to the left and right of a specific string by calling RTim and LTrim
function Trim(str)
{
        return RTrim(LTrim(str));
}
Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
java Script raj1_syed Javascript How-To 1 August 17th, 2006 01:49 PM
java script nalla ASP.NET 1.0 and 1.1 Professional 8 June 23rd, 2006 06:24 AM
java script [email protected] Javascript How-To 1 March 22nd, 2006 04:10 AM
Java vs Java Script functions joemorrison74 J2EE 0 July 6th, 2005 04:28 PM
java script zouky JSP Basics 1 September 27th, 2004 01:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.