By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

Taking out white space in string

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.