- 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
No, don't do that. This prevents me from copying and pasting the whole phone number in. Most implementations I've seen do not get the navigation right, so if I mistype the last number in one of those sections, I can't just hit backspace to correct it, I have to move back to the previous control and hit backspace.
Just do the simple replace in this case. Computers are really good at this sort of thing.
Admin
I like to call this method "the Plagiarizer"
Admin
But this function does not work, try it for yourself:
This breaks!
Admin
No it doesn't. According to the javadoc for java.lang.String
replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar
all occurences
Admin
Duh, that was in response to the statement:
removeSpecialChars( "a&b&c") returns "a b&c"
Admin
god i hope you're not serious.
seriously though it's very possible to end up with -1 there :)
Admin
Admin
If your app was an employee, what kind of employee would it be? When it's employee performance review time, how would you rate it?
Anal-Retentive Guy [image]
From CPU
Admin
I think the method in the post makes the most sense since its at least readable. Your regexp is wrong too.
Your regexp is effectively
which matches the characters ' " & or | since you placed them in a character class. There's no reason to escape both quote types in a character class either, nor do you need that last pipe if it wasn't in a character class. or would have matched what the original post did.For a language that requires quoting (like Java) that's
orAdmin
That regexp is still wrong. You want ["'&] using a | in a character class is a literal |, not a logical operator and there's no reason to escape quotes in a regexp, the only reason to escape them is because of a language forcing you to place the regexp in a quoted string.
or is what you wanted.Admin
This site needs a Digg-like system for voting whether or not this is actually a WTF.
My vote for this one is no.
Admin
while you are right if the replacement only occurs once in the application, for a repeated replacement like this one, regexp is much more efficient. by compiling a java Pattern object once with a regexp to do all your replacements, you can reach much better performane. there's a reason 'they' invented regexp.
Admin
sure, because when you use "replaceAll" , the method call generates a java "Pattern" object and from it you generate a Matcher object for the string. the compilation of a regexp Pattern in java is expensive. for efficient code you need to do: Pattern replacer = Pattern.compile(""[\\'|\\"|&|]"); Matcher matcher = replacer.Matcher(myString); String result = matcher.replaceAll(" ");
which is exactly what happens when you just use someString.replaceAll(pattern,somethingToReplaceWith);
by caching the Pattern, you can achieve much grater performance.
Admin
aah yeah. and you're going to tell the customers every day, how the input is typed correctly ?
Sorry, but whe thinking about similiar behaviour of some of my UI's, the standard "we're in 2007, man, why on hell is this not working as i expect it"-customer would sanitize me ;)
and always a great captcha: sanitarium LOL
Admin
The name of the replaceAll function of java.lang.String is misleading. replaceRegex would have been a better name. It's not WTF (worse than failure) though.
Admin
For a complex regex yes, but hardly for a straight replacement of a few predefined characters. Consider the following test class, the replaceCharacters method still outperforms replaceRegex with a factor of nearly 10 to 1 in my tests.
Of course if the replace were more complex or if you wanted to make it configurable, that would be another thing.
public class test {
}
Admin
Rubbish. I run a system which automatically ingests tv-program information into a Digital Object Management System. The program information contains special characters, the DOMS requires that we use pid's without special characters, so we remove them pretty much as shown in this "wtf".
The idea that we should reject programs containing non-standard characters would seriously compromise our data completeness, a key target in our system - essentially we'd fail to ingest a large fraction of our target programs, which would then be unavailable to our end-users.
Admin
I think few people thought it's a Good Code(tm). But being Bad Code, it's still far from the WTF level. It's just moderately ugly and a bit too rigid. For SQL query it may be dangerous. For XML maybe cleaner methods exist. Doing quick raw conversion before processing the text for stats or matching against patterns, preparing a string for full text search, or otherwise preparing the text for processing which analyses it, doesn't store/display it though, it may be perfectly okay. Especially if adding extra characters to the set would be bad, because, say, all others must be allowed.
Admin
Nope. Java not JavaScript.
Nope. As others have pointed out, the characters might be the only ones of concern for the context that this code is used in.
You're assuming this is data that's intended to be used in a webpage (in which case an escaping function such as fn:escapeXml for JSP expression language should be used).
Also depends on whether you precompile the regex up front. Too many people are used to Perl, where this is possibly done for you. Using the regex functions that come with many Unix systems libc, or the regex support in Java, you need to explicitly precompile the refex to avoid the overhead of donig it every time.
Chris
Admin
As I pointed out in another post, this is Java not JavaScript, and the String replace() methods replace each occurence of the character or substring, not just the first one.
Chris
Admin
Apart from the fact that it should be PHP : PHP :: JSP : Scriptlets, most people who know what they're doing don't embed Java code in JSP anymore. Ever since the standard tag library and expression language became available, it's good practice to disable scriptlet support in your web container.
Chris
Admin
I agree it's badly named. The replace() methods should be called replaceAll(), and the replaceAll() that currently takes a regex to be compiled into a pattern should be called replacePattern().
Chris
Admin
:) Made me laugh .... thx ...
p.s. In my defence, I'n not a native speaker ..
Admin
Wait a sec ... if the goal of the first two replacements is to replace single and double quotes with spaces, should he have used double quotes around his escaped quote marks instead of single ones?
Am I the only one who noticed this, or does Java not distinguish between single and double quotes?