Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

how to test a string if it contains special characters

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
titan nyquist - 30 May 2007 12:50 GMT
How do you test a string to see if it contains special characters?  I
want to ensure that any names typed into my form has only letters (and
maybe allow a dash and an apostrophe).

I can loop RealName.Contains("..."), but there must be a more elegant
solution.
Alberto Poblacion - 30 May 2007 12:57 GMT
> How do you test a string to see if it contains special characters?  I
> want to ensure that any names typed into my form has only letters (and
> maybe allow a dash and an apostrophe).
>
> I can loop RealName.Contains("..."), but there must be a more elegant
> solution.

  You can use a Regular Expression:

  RegEx re = new RegEx("^[-'a-zA-Z]*$");
  if (re.IsMatch(stringToTest)) ....//string is correct
titan nyquist - 30 May 2007 13:11 GMT
Thanks!

>    You can use a Regular Expression:
>
>    RegEx re = new RegEx("^[-'a-zA-Z]*$");
>    if (re.IsMatch(stringToTest)) ....//string is correct

Ok, do I have this right...

^ anchors to front
$ anchors to back
a-z = any char from a..z
A-Z = any char from A..Z
* = 0 or more of the preceeding

what does ' or -' do?
titan nyquist - 30 May 2007 13:12 GMT
> Ok, do I have this right...
>
[quoted text clipped - 5 lines]
>
> what does ' or -' do?

Oops...

^ = negates / negative
titan nyquist - 30 May 2007 13:14 GMT
> > what does ' or -' do?

I don't know where my head is... -' is just including ' and - to the
allowed character list.  Now I get it all.  Thank you!

Titan
Göran Andersson - 30 May 2007 19:29 GMT
>> Ok, do I have this right...
>>
[quoted text clipped - 9 lines]
>
> ^ = negates / negative

Outside a set ^ matches the start of the string, inside a set (as first
character) it negates the set.

Signature

Göran Andersson
_____
http://www.guffa.com

Alberto Poblacion - 30 May 2007 15:41 GMT
>>    RegEx re = new RegEx("^[-'a-zA-Z]*$");
>>    if (re.IsMatch(stringToTest)) ....//string is correct
[quoted text clipped - 8 lines]
>
> what does ' or -' do?

   You said that you might also allow dashes and apostrophes, so that's why
I added ' and -. If you only want letters, then leave just [a-zA-Z].

> Oops...
> ^ = negates / negative

   No, when the caret is used at the beginning of the expression your first
interpretation was right: Anchor at front.
titan nyquist - 30 May 2007 16:16 GMT
Thanks for clarifying!
Tim Sprout - 31 May 2007 04:54 GMT
> How do you test a string to see if it contains special characters?
> Iwant to ensure that any names typed into my form has only letters
> (and maybe allow a dash and an apostrophe).
>
> I can loop RealName.Contains("..."), but there must be a more
> elegant solution.

Another possible approach:

Create a Custom User Control TextBox and override OnKeyPress to
handle ASCII characters.

protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
       {
           // KeyChar property of the event Gets or Sets the
           // character corresponding to the key pressed
           // and returns the ASCII character that is composed.
           // Handled property of the event Gets or Sets a value
           // indicating whether the System.Windows.Forms.Control
           //      .KeyPress event was handled
           // and returns true if the event is handled; otherwise,
           // false

           int i = (int)e.KeyChar;    //cast KeyChar property to integer

           // Capital letters, small case letters, dash (or minus),
           // and apostrophe keys are not handled (handled returns
           // false) so are passed to the textBox and displayed.
           // All other KeyPresses are handled (handled returns true)
           // so are not passed to the textBox and are not displayed.

           if (i >= 65 && i <= 90 || i >= 96 && i <= 122 || i == 39)
           {
               e.Handled = false;
               return;
           }
               e.Handled = true;
       }

-Tim Sprout
Peter Duniho - 31 May 2007 05:01 GMT
>             if (i >= 65 && i <= 90 || i >= 96 && i <= 122 || i == 39)
>             {
>                 e.Handled = false;
>                 return;
>             }

If one is going to do it that way, IMHO it is better to not convert to  
integers.  Even if you just did "if (e.KeyChar >= 'A' && e.KeyChar <= 'Z'  
|| e.KeyChar >= 'a' && e.KeyChar <= 'z' || e.KeyChar == '\'' || e.KeyChar  
== '-')", that would be more readable and more maintainable.  Even better  
is that you can do "if (Char.IsLetter(e.KeyChar) || e.KeyChar == '\'' ||  
e.KeyChar == '-')", which is even more readable and maintainable.

(ignoring for the moment that your code leaves out one of the possible  
characters...the fact that I have to go to an ASCII table to figure out  
whether you forgot the apostrophe or the hyphen is an example of the lack  
of readability of your code :) ).

Pete
Tim Sprout - 31 May 2007 06:01 GMT
>             if (i >= 65 && i <= 90 || i >= 96 && i <= 122 || i == 39)
>             {
>                 e.Handled = false;
>                 return;
>             }

If one is going to do it that way, IMHO it is better to not convert to
integers.  Even if you just did "if (e.KeyChar >= 'A' && e.KeyChar <= 'Z'
|| e.KeyChar >= 'a' && e.KeyChar <= 'z' || e.KeyChar == '\'' || e.KeyChar
== '-')", that would be more readable and more maintainable.  Even better
is that you can do "if (Char.IsLetter(e.KeyChar) || e.KeyChar == '\'' ||
e.KeyChar == '-')", which is even more readable and maintainable.

(ignoring for the moment that your code leaves out one of the possible
characters...the fact that I have to go to an ASCII table to figure out
whether you forgot the apostrophe or the hyphen is an example of the lack
of readability of your code :) ).

Pete

Thanks for the comments, Pete!

-Tim Sprout
Mihai N. - 31 May 2007 08:33 GMT
> How do you test a string to see if it contains special characters?  I
> want to ensure that any names typed into my form has only letters (and
> maybe allow a dash and an apostrophe).

Be carefull what you check for. "letters" is language dependent.

If you test with (i >= 65 && i <= 90 || i >= 96 && i <= 122 || i == 39)
or regex ranges [a-zA-Z] or other such English-centric ideas, you will
affect other languages using characters outside these ranges.

And that is almost every language out there, with very few exceptions :-)

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Alberto Poblacion - 31 May 2007 09:37 GMT
> Be carefull what you check for. "letters" is language dependent.
>
[quoted text clipped - 3 lines]
>
> And that is almost every language out there, with very few exceptions :-)

   If you follow the Regular Expression route, you can test for characters
in Unicode groups and block ranges with the syntax \p{name}, where "name" is
a named character class. For instance, to test for all Alpha characters, you
use  \p{L}. Another useful one is \p{IsBasicLatin} (warning: case
sensitive), which tests for the Unicode range that includes the English
alphabet.
Peter Duniho - 31 May 2007 18:11 GMT
> Be carefull what you check for. "letters" is language dependent.
>
> If you test with (i >= 65 && i <= 90 || i >= 96 && i <= 122 || i == 39)
> or regex ranges [a-zA-Z] or other such English-centric ideas, you will
> affect other languages using characters outside these ranges.

Good point...one more good reason to use Char.IsLetter() instead of  
explicitly checking the character code.  :)

I also appreciate Alberto's information...I had no idea you could test for  
specific Unicode character flags using regular expressions.  Very cool.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.