- 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
If only there was a method to replace all occurrences of a string (such as " ") with an empty string. Someone should get on that.
Admin
I see only one comment on that post for the frist time
Admin
Is string unwanted 1,2,3 or 10 char long? Im amazed how you an spend time and figure out something like that and Not start thinking that this might not be the right way doing it.
Admin
Admin
Fixed?
Admin
CLEARLY the dev should have loaded them into an array...
Admin
// fixed?
Admin
FTFY 4.0
Admin
Admin
The second function looks like the result of adding more Replaces until all the test strings came through as expected.
Admin
FTFY
Admin
XML doesn't care about spaces. Why should a developer?
Admin
Well, up to five spaces in an input field is clearly a typo and a mistake. But if the user puts SIX spaces in the field, they must have really meant it, so the field hasn't been left blank because it now contains what the user intends.
Admin
What about if there are embedded tabs and such in addition to spaces? There has got to be some regular way of expressing this.
Admin
Almost, you forgot the all important
which is required in order to prevent people from attempting to read past the end of the text. Think of it as an EOF for humans.
Admin
I think I have the perfect solution for PHP:
Double linefeeds are not so perfect, but I think it kinda makes sense with this class.
Admin
public boolean isBlank(String str) { return String.IsNullOrEmpty(str.trim()); }
Admin
Admin
Please don't misuse try/catch for null-checking.
/facepalm
Admin
This is easy to fix:
Admin
Everybody, stop reading! This is the last comment!
//end
Admin
I've actually used/seen C code with //eof on the last line of every file in a project.
Admin
/facepalm2
Admin
public boolean isBlank( String str ) {
}
I am amazed by the fact that most Java developers dont know (or dont care) about commons-lang (or apache commons in general)
Admin
You're all ripping on the first example, which is fair. But the second one may actually be kind of clever:
Suppose your requirement is you need to remove extra spaces, but not all spaces; you can't just do " "->"", and if you don't have regular expressions handy, this solution is not that bad.
It first replaces spaces 10-for-1, twice. So unless the original string had more than 900 spaces in a row, it now has at most 9 in a row in any one place. Next it replaces 3-for-1, so any 9-space chunks become 3-space chunks. Next it does 2-for-1 twice, so those 3-space chunks become 2 and then 1-space. And now there are no extra spaces anywhere, but there are also still single spaces everywhere there should be.
So with that second example, TRWTF is older versions of VB which lacked regular expressions, forcing this kind of solution. But the solution itself is valid.
Admin
Admin
"So this string is blank? "
Admin
You must be using a different definition of "clever" than the rest of us.
Admin
I have manually done similar tricks in slow text editors when dealing with large files. Here was my manual algorithm:
Admin
Space is big. Really big. You just won't believe how vastly, hugely, mindbogglingly big it is. I mean, you may think it's a long way down the road to the chemist's, but that's just peanuts to space
Admin
So you tell me, how do you replace all occurrences of 2 or more spaces with only 1 space, in VB6, with no regular expressions?
I even googled it, and the suggested solutions include "copy the string char-by-char, skipping space chars that come after other space chars"; "while string contains ' ' replace ' '->' '".. this solution is probably faster than those, at least.
Admin
Admin
Admin
Admin
Admin
The RemoveCharacters function is interesting, it was clearly built to reduce the number of Replace calls.
It handles all cases up to 208 contiguous spaces. From 209 to 1200 it becomes progressively worse. 1200 contiguous spaces is the largest it handles correctly. It correctly handles 58.7% (704) of the cases up to 1200 spaces.
Admin
Admin
trim() only returns a new object if it needs to... otherwise it returns this
return ((st > 0) || (len < count)) ? substring(st, len) : this;
If you want to handle NullPointerException yourself then just remove the try/catch.
Admin
How many WTFs can you find in this code?
Admin
You must have failed algorithmic complexity. This may be an attempt at efficiency, but it's a failed attempt because you ignore any complexity of the Replace function. Iterating over every character in the string and skipping whitespace characters will only have to iterate over the length of the string once. The 'replace blocks at a time' will have to iterate over the string for each set of blocks it replaces. I suspect, although I'm not a VB expert (thank god), that it will also allocate a comparatively large amount of additional memory in comparing and storing the various intermediate strings.
Admin
This is probably due to certain older compilers (cough Watcom cough) that would choke on EOF in certain situations, making this sort of thing a best practice.
Admin
Criticizing their code would be too easy. So I'm going for the comments:
Wow, thank god he defined the data types in the comments. As if I couldn't read them in the code. And it's really too bad that Javadoc can't figure it out automatically.
Replace characters as spaces. Right. Not replace spaces with spaces, as the code does. So... WTF is strUnwanted?
Admin
Sure it does what it needs to without regular expressions, which conceivably could be a real restriction, but as soon as someone needs to maintain it, they have to spend some time figuring out what it actually does, and whether or not it's working properly. Something less efficient that is more obvious in how it works would probably have been a better choice.
But then, I'm probably preaching to the choir.
Admin
Sounds like the way I use clever... as in "if you find yourself writing particularly clever code, you should probably stop and think about what you're doing wrong and/or what you missed".
Admin
I dunno maybe use Split() and manually join all non-empty elements with a space in between each one?
Admin
The apache commons libs are the most awsome libs out there!
Btw i love replacing replacing replacing stuff.
Admin
Admin
Yes, but this function discounts all whitespace. You changed the spec.
Admin
Doing a while-string-contains loop may be marginally slower (most likely a tiny fraction of a second slower) than the method in today's post, but it will work reliably, and is easy to understand even without comments. That makes it vastly superior to the posted method, which is unreliable and difficult to understand.
VB6 may not have built-in support for regular expressions, but VBScript does, and VB6 can reference the VBScript library to allow coders to use regular expressions - see http://www.regular-expressions.info/vb.html - so the "solution" is still boneheaded both from that perspective as well as the reason I mentioned in my first paragraph.
Admin