473,385 Members | 1,907 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,385 software developers and data experts.

Taking out white space in string

I am trying to develop a script that takes out the white space at the
FRONT and the BACK of a string but not the middle. I have a string
that looks like this:

XXXX XXXX XXXX XXXX

The groupings within the string can be any length...however, in order
to dump this data into a SQL database, the potential added white space
at the front or the back of the string needs to be deleted as the user
moves from one text box in a form to the next.

Any ideas?
Jul 20 '05 #1
5 38106
Ashlie wrote on 08 jul 2003 in comp.lang.javascript:
I am trying to develop a script that takes out the white space at the
FRONT and the BACK of a string but not the middle. I have a string
that looks like this:

XXXX XXXX XXXX XXXX

The groupings within the string can be any length...however, in order
to dump this data into a SQL database, the potential added white space
at the front or the back of the string needs to be deleted as the user
moves from one text box in a form to the next.

<script>
function trim(s){
return s.replace(/^\s*(.*?)\s*$/,"$1")
}

alert(">"+trim(' XXXX XXXX XXXX XXXX ')+"<")
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2
> I am trying to develop a script that takes out the white space at the
FRONT and the BACK of a string but not the middle. I have a string
that looks like this:

XXXX XXXX XXXX XXXX

The groupings within the string can be any length...however, in order
to dump this data into a SQL database, the potential added white space
at the front or the back of the string needs to be deleted as the user
moves from one text box in a form to the next.


Check out the trim method here:
http://www.crockford.com/javascript/remedial.html

Jul 20 '05 #3
This works fine for me,

trim(stringValue)
{
return stringValue.replace(/(^\s*|\s*$)/, "");
}

Douglas Crockford wrote:
I am trying to develop a script that takes out the white space at the
FRONT and the BACK of a string but not the middle. I have a string
that looks like this:

XXXX XXXX XXXX XXXX

The groupings within the string can be any length...however, in order
to dump this data into a SQL database, the potential added white space
at the front or the back of the string needs to be deleted as the user
moves from one text box in a form to the next.

Check out the trim method here:
http://www.crockford.com/javascript/remedial.html


Jul 20 '05 #4
frogcoder <se*****@ms77.url.com.tw> writes:

Please don't top post.
This works fine for me,

trim(stringValue)
{
return stringValue.replace(/(^\s*|\s*$)/, "");
}


Does it now?
What is the result of
trim(" foo bar ")
?
It only removes whitespace before *or* after the string, not both.
It can be fixed simply by adding a "g" after the regexp:
/(^\s+|\s+$)/g
(uses + too, there is no reason to replace nothing)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
JRS: In article <bh********@netnews.hinet.net>, seen in
news:comp.lang.javascript, frogcoder <se*****@ms77.url.com.tw> posted at
Sun, 17 Aug 2003 17:24:25 :-
This works fine for me,

trim(stringValue)
{
return stringValue.replace(/(^\s*|\s*$)/, "");
}


See FAQ sec 4.16 for a correct way of removing both leading & trailing
white-space.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Grumble | last post by:
Hello all, I want to read lines from a text file, where each line has the following syntax: token1:token2:token3 There could be white space between tokens and ':'
4
by: ucfcpegirl06 | last post by:
Hi, I need help getting rid of trailing white spaces. I am searching a file for various data (not important) and retrieving it. I output the data if found to a file. An example would be:...
0
by: Tom Jastrzebski | last post by:
Hello everybody, It looks like this is a known problem, but I found no solution. Deserialization of DataSet object from XML does not preserve white space. The same code executed under .Net...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
2
by: shagy | last post by:
Hi, I'm having a problem with a <select><option> which has white space in values... When I post the data I only get the first word (up to the white space). "Testing white space" becomes...
12
by: JA | last post by:
Is there a way to remove all the white space in the fields? I have been using Find-and-replace - looking for 2 or 3 or 4 or 10 spaces and replacing them with none. I don't want to replace single...
12
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function...
5
by: Richard Maher | last post by:
Hi, Can someone please show me how to tell Firefox to preseve white-space when returning the selectList.option.value attribute? I have change the style so that the white-space is preserved on...
4
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.