char.IsLetter not a good test to exclude numbers

char.IsLetter not a good test to exclude numbers

Old forum URL: forums.lhotka.net/forums/t/9322.aspx


Jav posted on Wednesday, August 04, 2010

SampleApp has very worthwhile examples of sync and async BusinessRule to check for the presence of numeric digits in strings. Unfortunately any string with a more that one word will returrn positive, i.e., the rule is broken.  Here is the code:

        bool result = string.IsNullOrEmpty(ce.Name) ||
          !(from c in ce.Name.ToCharArray()
            where !char.IsLetter(c)
            select c)
            .Any();

It appears that char.IsLetter() considers a space a number.  For example, Good Morning returns a broken rule.  My C# book says char.IsLetter considers only A-Z and a-z as letters, so that will exclude commas, periods, semicolons, etc also from the "Letter" category. char.IsDigit (positive if 0-9) might be a better test.

Jav

RockfordLhotka replied on Wednesday, August 04, 2010

Thanks, that's good to know. Of course the sample isn't intended to have a great rule for detecting numerics - just to show how to build sync/async rules :)

Fortunately I'm messing with that sample at the moment though (enhancing it for a talk I'm giving at the Visual Studio Live! conference), so I'll see about making this change.

Jav replied on Wednesday, August 04, 2010

It came about when I entered O'Donnel in the name box.  With the old style rules, I wouldn't have given it a second thought.  I was so impressed with the new rule system as I was trying to dissect it to see how it worked, and what exactly was causing the error.  It was my own surprize in discovering that a space character is not considered a letter that I felt I should share it.

Of course the name of the rule is StringOnlyLetters, so technically char.IsLetter is not the "wrong" test in the rule. Smile

Jav

Copyright (c) Marimer LLC