473,840 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?

-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 4+) :

String.prototyp e.LTrim=new Function("retur n this.replace(/^\\s+/,'')")
String.prototyp e.RTrim=new Function("retur n this.replace(/\\s+$/,'')")
String.prototyp e.Trim=
new Function("retur n this.replace(/^\\s+|\\s+$/g,'')")

or for all versions (trims characters ASCII<32 not true
"whitespace "):

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>=0 && str.charAt(j)<= " " ; j--) ;
return str.substring(0 ,j+1);
}
function Trim(str) {
return LTrim(RTrim(str ));
}

http://msdn.microsoft.com/library/de...63906a7353.asp

http://docs.sun.com/source/816-6408-10/regexp.htm
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javas cript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.

Feb 22 '07 #1
27 6886
FAQ server wrote:
-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 4+) :

String.prototyp e.LTrim=new Function("retur n this.replace(/^\\s+/,'')")
String.prototyp e.RTrim=new Function("retur n this.replace(/\\s+$/,'')")
String.prototyp e.Trim=
new Function("retur n this.replace(/^\\s+|\\s+$/g,'')")
What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.

I don't have it at hand but someone here kindly wrote one for me.

Andrew Poulos
Feb 22 '07 #2
Andrew Poulos wrote on 22 feb 2007 in comp.lang.javas cript:
FAQ server wrote:
>-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 4+) :

String.prototyp e.LTrim=new Function("retur n this.replace(/^\\s+/,'')")
String.prototyp e.RTrim=new Function("retur n this.replace(/\\s+$/,'')")
String.prototyp e.Trim=
new Function("retur n this.replace(/^\\s+|\\s+$/g,'')")

What would also be useful is another prototype that reduces multiple
contiguous spaces to a single space.
String.prototyp e.whiteSpaces2s ingleSpace =
new Function("retur n this.replace(/\\s\\s+/g,' ')")

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #3
Evertjan. <ex************ **@interxnl.net wrote:
String.prototyp e.whiteSpaces2s ingleSpace =
new Function("retur n this.replace(/\\s\\s+/g,' ')")

writing :

....(/\s\s+/g,' ')...

isn't sufficient no need to escape \ ???

ami i wrong ?
--
Une Bévue
Feb 22 '07 #4
Une Bévue wrote on 22 feb 2007 in comp.lang.javas cript:
Evertjan. <ex************ **@interxnl.net wrote:
>String.prototy pe.whiteSpaces2 singleSpace =
new Function("retur n this.replace(/\\s\\s+/g,' ')")


writing :

...(/\s\s+/g,' ')...

isn't sufficient no need to escape \ ???

ami i wrong ?
Did you try?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #5
Evertjan. <ex************ **@interxnl.net wrote:
Did you try?
YES here it is :

<http://www.yvon-thoraval.com/JS/test_regexp.htm l>

and i'm TRUE ;-)
--
Une Bévue
Feb 22 '07 #6
Une Bévue wrote on 22 feb 2007 in comp.lang.javas cript:
Evertjan. <ex************ **@interxnl.net wrote:
>Did you try?

YES here it is :

<http://www.yvon-thoraval.com/JS/test_regexp.htm l>
Nonsense, you did not try,
but did quite someting else!

You did this:

var s = p.replace(/\s\s+/g, ' ');

While my line was:

String.prototyp e.whiteSpaces2s ingleSpace =
new Function("retur n this.replace(/\\s\\s+/g,' ')")

Please try that with /\\s\\s+/g replaced by /\s\s+/g

.... and you will see what goes wrong.

Do you know what goes wrong?
and i'm TRUE ;-)
Are you Boolean?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #7
Evertjan. <ex************ **@interxnl.net wrote:
Please try that with /\\s\\s+/g replaced by /\s\s+/g
i have added the /\\s\\s+/g for t right now
Do you know what goes wrong?
Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototyp e.whiteSpace2si ngleSpace implies ???
--
Une Bévue
Feb 22 '07 #8
In article <1h************ *************** *******@google. com.invalid>,
un************@ google.com.invalid (Une Bévue) wrote:
Evertjan. <ex************ **@interxnl.net wrote:
Please try that with /\\s\\s+/g replaced by /\s\s+/g

i have added the /\\s\\s+/g for t right now
Do you know what goes wrong?

Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototyp e.whiteSpace2si ngleSpace implies ???
Safari doesn't like your toolkit.js as there is an extra , on line 348
or so. FF didn't seem to mind.

-- tim
Feb 22 '07 #9
Une Bévue wrote on 22 feb 2007 in comp.lang.javas cript:
Evertjan. <ex************ **@interxnl.net wrote:
>Please try that with /\\s\\s+/g replaced by /\s\s+/g

i have added the /\\s\\s+/g for t right now
>Do you know what goes wrong?

Not at all because i thought the purpose of the Regexp was to replace

multiple white spaces by single one, as the name :

String.prototyp e.whiteSpace2si ngleSpace implies ???
You are on the wrong track.
>String.prototy pe.whiteSpaces2 singleSpace =
new Function("retur n this.replace(/\\s\\s+/g,' ')");
new Function() here has a string as parameter,
and in javascript strings backslashes are used as escape characters.
If you need the backslashes, you will have to escape them themselves.
This is ONLY required in a string,
not in a regex declaration without a string.

Try this:

=============== =============== ======
<script type='text/javascript'>

alert("return this.replace(/\s\s+/g,' ')");

alert("return this.replace(/\\s\\s+/g,' ')");

</script>
=============== =============== =======


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 22 '07 #10

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

Similar topics

31
2929
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char *trim(char *s) { char *begin,*end; begin = s;
12
1400
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.lTrim = function() { return this.replace(/^\s+/,'');
0
9856
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9436
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7836
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.