Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

trim function in Javascript

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to trim the string entered by user in html form by using javascript ..javascript does not have trim function like vbscript has..can anyone tell me about it's alternative..?
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try using replace:
ltrim = /^\s+/ ;
rtrim = /\s+$/ ;
myString.replace( ltrim , '') ;
myString.replace( rtrim , '') ;
I haven't actually tried this, so I'm not sure if the RE syntax is correct, but it should be a start ... hope this helps.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the following script at javascript.internet.com:

This snippet just rebuilds your string, one character at a time, leaving out the leading spaces. You could do something similar for trailing spaces.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'va already made trim function in JavaScript.
Below is the code, you can use it for free:
function trim (s)
{
var iLen = s.length;
var sOut = "";
var chr = "";

for (var i=0; i<iLen; i++)
{
chr = s.charAt (i);
if (chr!=" ")
{
sOut = sOut + chr;
}
}
return sOut;
}


------------------
Put whatever your information needs in this multipurpose outlining software (www.ezoutliner.com)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made the following funcs based on the logic in Bodie's post:
function LTrim (s){
//trim lefthand spaces
while(''+s.charAt(0)==' ') {
s = s.substring(1,s.length-1);
}
return s;
}
function RTrim (s){
//trim righthand spaces
if (s.length > 1) {
while(''+s.charAt(s.length-1)==' ') {
s = s.substring(0,s.length-2);
}
return s;
}
}
+++++++++++++++++++++++++++++++++++++++++++

Originally posted by Bodie Minster:
[B]I found the following script at javascript.internet.com:

This snippet just rebuilds your string, one character at a time, leaving out the leading spaces. You could do something similar for trailing spaces.[/B]


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works fine!
while(text.search(/\s/) > -1)
{
text = text.replace(/\s/, "");
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mayasari soedarmo, just wanted to let you know that your function doesn't trim, it simply takes out all spaces. Just in case you want to share more information that isn't free. ;-)

[This message has been edited by Martin Roy (edited August 21, 2001).]
 
Martin Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a few minor ajustments to tomstar's RTrim and LTrim functions, the following are tested and working in my application.
Thanks for the help. It saved me a lot of time
function LTrim (s){
//trim lefthand spaces
while(''+s.charAt(0)==' ')
{
s = s.substring(1,s.length);
}
return s;
}
function RTrim (s)
{
//trim righthand spaces
if (s.length > 1)
{
while(''+s.charAt(s.length-1)==' ')
{
s = s.substring(0,s.length-1);
}
}
return s;
}
function trim (str)
{
str=LTrim(str);
str=RTrim(str);
return str;
}


[This message has been edited by Martin Roy (edited August 21, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those scripts that have loops are a good idea;
but they are certainly not optimized.
I think it is easier to check the front and the back and exit when u reach a character.
Make sure u check for the case of empty string first else u forever stay in the loop....
=)
 
reply
    Bookmark Topic Watch Topic
  • New Topic