- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
python:
C++:
Java:
It's a long time ago since I wrote something in VB, but I think this should do it (I run Linux here, so I can't verify it):
Admin
Yes, however strings are immutable. Since removing spaces changes the string, new strings are created. The original string remains the same.
Admin
Sorry if this isn't original, I've only read the first two pages of comments. (But I have searched for "Unicode" in the other pages and didn't find anything.)
Python, using the Unicode character database to see which characters are spaces:
Admin
Actually this code ("lazy" version; python 2.4):
Might be approximately similar performant then:
Maybe even faster, because it don't has to append all those empty strings when a space is found. ;) But on the other hand replace is implemented in C and therefore faster.
Admin
[quote user="Matt Nordhofff"]Sorry if this isn't original, I've only read the first two pages of comments. (But I have searched for "Unicode" in the other pages and didn't find anything.)
Python, using the Unicode character database to see which characters are spaces:
But doesn't do the isspace() method the same thing for str and unicode objects? And so I can write a generic function which will accept str and unicode:
Admin
Erm, the last comment of mine is messed up. Sorry. The quote is broken and a bit of text I wrote is missing: Your implementation is O(n^2) because of the +=. With the join method O(n) is possible, while still using the unicode library.
Admin
C is C++ subset.
I'd first complain that in-place string operations are oftend BAD IDEA (strings passed as parameters, for example).
Admin
Aaargh! You must be kidding!
Admin
remove_if only makes sense if you actually need a predicate function (or functor), e.g.
Admin
(This post is in reply to panzi's comments about my silly Python Unicode character database implementation.)
Well, the point was to be silly, not to be O(n). :P
Your first implementation looks good, except you should use ''.join(...) instead of u''.join(...) so that the returned string won't be a Unicode string unless the string passed to it is. Your second implementation (with str.isspace()) ruins the fun.
Interesting to know that using += makes it O(n^2), though. Python FastCGI scripts have to return all of the output at once (or use yield), so I do a lot of +=ing in them.
Admin
ABAP CONDENSE <fieldname> NO-GAPS.
Admin
The real question to me is why does RemoveSpace remove all of the Spaces??? Should it be RemoveSpaces?
Some Perl implementations:
This works for something simple like:
It will of course fail if you use 'quoted strings' in the cmd line arguments, but you should know how to use your tools...
The above may have been mentioned but it a unique method that I thought of after people used a straight replace regex. I did learn about tr/ //g, And a person mentioned that it is faster (I have yet to research this) then s/ //g
This has been a great read! Some really cool methods.
Admin
This avoids needing any special code to append the remaining '\0' on the end, since the do-while loop will always run an "extra" time.
Admin
This will loop until from is the NULL pointer. Apart from that, the special case code for the empty string is completely redundant.
Admin
To all the arrogant half-wits who criticize the idea of putting the r-value on the left, consider this:
bool x; ... if (x = true) { return; } ...
GCC does not catch that (I realize that comparing with true/false is absurd, but I've seen it in a lot of code not written by me). Also, why not put the r-value on the left? It's perfectly legal C/C++ and does, under some circumstances, save you from writing bugs. I never could adopt the habit, maybe because I've never been bitten by this problem, but why the hostility? You complain that people should know the difference between = and ==, yet you can't seem to bring yourselves to understand the concept of commutativity.
Of course, it does fail, as you say, when comparing two l-values, but it does catch many problems.
As for the comment about the original code being "reasonably efficient", it's not. It does string concatenation, which doesn't work so well in VB. A better way would be to use Space$ or similar to allocate a string big enough to hold the output buffer, then use Mid$(...) = Mid$(...) to assign to the string, then RTrim$ the result before returning.
Admin
That's not obfuscated. That's ... SWEET !
Admin
Ohh, the best Perl script: y/// is the same as tr///! y/ //d
Admin
Admin
Here's a dandy in Ruby, that I just wrote up.
Rather than go the direct approach (like "'remove my spaces'.tr(' ','')") this will do the same thing but in the process will also,
Man, it's hard to write code this convoluted, but fun!
A typical example,
But be sure to have a good machine to run this on if you are removing spaces from a string with more than like 23 some odd characters. It quickly gets out of hand and just gets killed on my iron.
Admin
$no_whitespace_str = join "", grep /[^ ]/, split // for $str;
... but, as with all these examples, WHY?
Admin
a Haskell version without lib calls (actually works!):
and for removing extra spaces:
Admin
"I don't even know what the hell the Try/Catch is supposed to be doing"
It's because once a space is removed the string length changes and (assuming the FOR statement doesn't evaluate the range end each time) this will lead to an array index overflow...
Admin
your code is sucked!!!!!!!!!!!
Admin
The greatest WTF is the first implementation (in VB.NET) accomplishes exactly nothing.
If you check the code, it's only concatenating the spaces (not the useful data) into a string variable local to the function... He might as well "refactor" it as:
And my implementation is even more efficient, it returns in constant time! LOL
Abysmal...
Admin
Easier way in Ruby to remove whitespaces in a string:
Admin
pharmacie en ligne livraison europe https://kamagraenligne.shop/# Achat mГ©dicament en ligne fiable