Re: Is there any function as Trim in Jscript/JavaScript?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Randy Webb (hikksnotathome_at_aol.com)
Date: 06/24/04


Date: Thu, 24 Jun 2004 19:45:19 -0400

Vilmar Brazão de Oliveira wrote:
> Hi,
>
> Is there any function as Trim in Jscript/JavaScript?
> I look at Ms-JScript tutoria which comes with ms-visual studio 6.0, but did
> not find anything untill now.
> I try to check the length, but it returns me size when I put
> a string with some or a lot of white spaces.
> In VB or VBScript the funciont Len with a string with some or a lot of
> white spaces BRINGS ME 0()ZERO!

http://www.jibbering.com/faq/#FAQ4_16

Gives:

function LTrim(str) {
  for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
  return str.substring(k,str.length);
}
function RTrim(str) {
  for (var j=str.length-1; j&gt;=0 && str.charAt(j)<=" " ; j--) ;
  return str.substring(0,j+1);
}
function Trim(str) {
  return LTrim(RTrim(str));
}

And then you simply Trim(YourStringName)

-- 
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/


Relevant Pages

  • Is there any function as Trim in Jscript/JavaScript?
    ... I look at Ms-JScript tutoria which comes with ms-visual studio 6.0, ... not find anything untill now. ... a string with some or a lot of white spaces. ...
    (microsoft.public.scripting.jscript)
  • Re: Is there any function as Trim in Jscript/JavaScript?
    ... > Is there any function as Trim in Jscript/JavaScript? ... > not find anything untill now. ... > a string with some or a lot of white spaces. ...
    (microsoft.public.scripting.jscript)
  • Re: How can i Right Trim all the spaces of a very long (2000 chars) Charecter string ?
    ... > Right Trim Function in c,using Binary search kind of fast algorithm? ... > Start from the right most charecter of the string to the left of the ... Is the string allowed to contain white spaces in the significant part? ...
    (comp.lang.c)
  • Re: VB help
    ... retrieving the a srting value in the database through a record ... retrieved string shows some junk charecters in place of white spaces ... string in the database 8a Church Road Barry ...
    (microsoft.public.vb.general.discussion)
  • Re: Regex for white space
    ... > Suppose a series of white spaces in between a string, ... > Now I wonder how can I get a new string contains no spaces or substuted by symbols like _ ... Lew Pitcher, IT Specialist, Enterprise Data Systems ...
    (comp.unix.shell)