473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

how to use trim() in javascript

18
i used the below code for trimming..

Expand|Select|Wrap|Line Numbers
  1.   var str = "    blah blah   ";
  2.                         var trimmed = str.replace(/^\s+|\s+$/g, '') ;                        
  3.                         alert(trimmed);
  4.                         alert(trimmed.value.length);
its aint working.. i think small error..
can any1 retify..
thanks in advance..
Mar 25 '08 #1
5 17565
aswath
18
i used the bolow example.. none of it is worinking for trimming white spaces

Expand|Select|Wrap|Line Numbers
  1.                        var a = String.trim("        Visit W3Schools ");
  2.                         alert(a)
  3.  
  4.  
  5.                         var sMyVar = new String (" testing trim ");
  6.                         alert(sMyVar.trim());
  7.  
  8.  
  9.                         var str = "    blah blah   ";
  10.                         var trimmed = str.replace(/^\s+|\s+$/g, '') ;                        
  11.                         alert(trimmed);
  12.                         alert(trimmed.value.length);
  13.                         q=str.trim();
  14.                         alert(q);
  15.                         alert("'" + str.trim() + "'");
all my need is to take off white spaces before and after a string but not inbetween a string.. ie.,
'' babe babe " should be ''babe babe"......

any help..
thanks in advance..
regards.
aswath.n.s
(knowledge is power)
Mar 25 '08 #2
gits
5,390 Expert Mod 4TB
it works - just use:

Expand|Select|Wrap|Line Numbers
  1. alert(trimmed.length);
kind regards
Mar 25 '08 #3
gits
5,390 Expert Mod 4TB
threads merged - since they cover the same topic ... please don't multipost the same questions ...

kind regards
Mar 25 '08 #4
acoder
16,027 Expert Mod 8TB
Just a note that JavaScript strings don't have a trim() method, though you can add it using String.prototype.
Mar 25 '08 #5
mrhoo
428 256MB
this method removes leading and trailing whitespace from a string.
The second replace replaces multiple consecutive spaces (but not tabs or newlines) with single spaces-
take it out if you don't want that behavior.

Expand|Select|Wrap|Line Numbers
  1. String.prototype.clean:function(){
  2.     var str= this.replace(/(^\s+)|(\s+$)/g,'');
  3.     return str.replace(/ {2,}/g,' ');
  4. }
Mar 25 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RobG | last post by:
There is an interseting examination of varions trim functions here: <URL: http://blog.stevenlevithan.com/archives/faster-trim-javascript > The best all-round function seems to be trim3 when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.