- 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
All the world's a string.
Or a SubString... at the very least.
Admin
Sometimes a regexp really is the right answer ...
Admin
Wow, so many inefficiencies I don't even know where to start!
Ugh. Maybe the array is used in case sometime in the future, a new number is added between '4' and '5'.
Admin
Ooh! Ooh! Pick me, I know!
Is it when they use strings as booleans?
(I'm thinking the use of the .Add() method and the .Length field has to be a simply [s]typo[/s]Microsoft being helpful... picking on them for that would be shooting ducks in a barrel)
Admin
Wait, WTF??? bool validChars = true; and then 4 lines later: string matchedChar = "N";
Admin
Somebody has a very confused, multipersonal mind. First, as stated, sometimes RegEx really is the answer. Second, there are too many different styles in the code. As noted, using bool and string in the same method to indicate true/false.
I half expect that the Caller probably sets a string variable and then through tedious manipulation turns it back into a boolean value.
Admin
[s]There is no String, only Bool[/s]There is no Bool, only String
Admin
Admin
I think a loop with said "c >= '0' and c <= '9'" would beat the RegExp performance-wise.
Admin
Maybe this function was originally a system call.
That would explain the return value of "0" on success and "1" on failure.
Admin
This assumes that the string fits into a 32 bit signed integer (–2147483648 to 2147483647). An international phone number or some kind of account number could already exceed that limit.
Admin
Surely int.parse will limit valid results to under 2147483647
Admin
Hey, you know student code is not a WTF! And this can't have been written by anyone with more than a week of programming experience. Right? Please?
Or do you hire linguistic majors for your programming?
Admin
Admin
I meant "system call" in the sense of a call to an outside process like a BASH script. Sorry -- haven't been fully caffeinated yet.
Admin
Admin
Admin
Um, I think you'r forgetting about the "11" in 911.
Admin
"Ugh. Maybe the array is used in case sometime in the future, a new number is added between '4' and '5'."
I think I'll change my phone number to 07709 34724½8¾. No way telemarketers will find me then!
Admin
WTF is "Sting Verification"?
Admin
My 2 year old seems to thing 6 comes between 4 and 5...
Admin
We had fun with this one at work. Here's my version:
private boolean checkChars(string destination) { int cnt = 0; if (destination != null) { for (int i = 0; i < destination.length(); i++) { (destination.charAt(i) == '0') ? cnt++ : (destination.charAt(i) == '1') ? cnt++ : (destination.charAt(i) == '2') ? cnt++ : (destination.charAt(i) == '3') ? cnt++ : (destination.charAt(i) == '4') ? cnt++ : (destination.charAt(i) == '5') ? cnt++ : (destination.charAt(i) == '6') ? cnt++ : (destination.charAt(i) == '7') ? cnt++ : (destination.charAt(i) == '8') ? cnt++ : (destination.charAt(i) == '9') ? cnt++ : cnt = cnt; } } return (destination != null && cnt == destination.length()) ? true : false; }
Admin
Admin
-Harrow.
Admin
If only the validity of characters I would do something like:
using System.Text.RegularExpressions;
public class Blah { private static readonly Regex CharCheck = new Regex("[^0-9]"); private bool CheckCharacters(string destination) { return !(CharCheck.Match(destination).Success); } }
Syntax looked C#'ish, so wrote in that syntax, but similar could be done in Java.
Making it static means only one instance of the regex running. If used a lot, I'd probably add the compiled flag. In practice I haven't seen any threading issues with doing similar, but I would double check for a critical application since only the static methods are guaranteed thread-safe (Match is used as an instance method in this case). The readonly flag should help the compiler with optimizations.
If the logging is needed, just modify to keep the value of success, and use the Matches method instead: iterate thru the returned collection, logging as appropriate.
Just my two-cents.
Admin
Just wow! And replacing this with a single line:
(And yes, it is d* rather than d+; there is no requirement for the string to be non-empty.)
Admin
Admin
Hi Depricated, are you free for lunch on the Fouivth of Smarch?
Admin
(Yeah, and none of them includes FILE_NOT_FOUND, blah.)
Admin
Mmmm...I love me some recursion:
Admin
This looks like pre-optimization code. You write functions like this so you can rewrite them in the future when the boss demands the code runs faster and uses less memory. This is more clever than using some sort of wait function, as it really does increase the CPU rather than artificially slowing it down.
Admin
return Regex.Match("[\d]", destination).Success;
Admin
Written by the guy who gets payed per log line checked?
Admin
C# with extension method and lambda:
Example:
string foo = "abc123"; if (foo.ContainsNonDigits()) { Blah, blah, blah... }
Admin
A man once mad regular expressions. That apparently wasn't this man. Honestly dropping this in my list of "stuff you do without regex"
Admin
... or public if you want to use it outside the class it's defined in. :)
Admin
Not much work at your work, I see.
Admin
Better to do: return destination.Any(c => !char.IsDigit(c));
IsDigit never forgets that 0 is a digit too, even though it is also < '1'.
Admin
But I want to call it with Ⅷ.
Admin
i did some actual testing of this one (in Java, see code below) and the results really speak for themselves (each method called 2M times):
Enterprisey: 19858 simple: 192
Admin
Why not ask Zoidberg and usw
if (Regex.IsMatch(input, "^[0-9 ]+$")) return true;
Admin
Its not between 4 and 5 it's between 3 and 4 and its called bleem:
http://www.strangehorizons.com/2000/20001120/secret_number.shtml
Now that you know they'll try and kill you to keep it secret!
Admin
You left out the logging. Logging was clearly specified in the requirements document.
Admin
This could also be hugely convenient at some point in the future if 7 8 9!
Admin
Or just regex /\d+/
Admin
Admin
Years ago when I started teaching, I already knew programmers would just code the very first thing that occurred to them to solve a problem. I think I had a small measure of success when I told them "If you can't think of at least two ways to do what you're trying to do, you're not actually thinking about the problem. Sure, you might think of some really bad way to solve the problem, but at least you've got something to compare your other idea to. You can improve from there."
Admin
Admin
I fear that performance-wise it doesn't even matter.
Admin
Add logging and change the return type if you need to but the basic functionality is:
return s.ToCharArray().Any(c => "1234567890".Contains(c) == false);