473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to test a string if it contains special characters

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.

May 30 '07 #1
13 20689
"titan nyquist" <ti***********@gmail.comwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
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
May 30 '07 #2
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?

May 30 '07 #3
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?
Oops...

^ = negates / negative

May 30 '07 #4
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

May 30 '07 #5

"titan nyquist" <ti***********@gmail.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
> 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?
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.

May 30 '07 #6
Thanks for clarifying!

May 30 '07 #7
titan nyquist wrote:
>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?

Oops...

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

--
Göran Andersson
_____
http://www.guffa.com
May 30 '07 #8
"titan nyquist" wrote:
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
May 31 '07 #9
On Wed, 30 May 2007 20:54:54 -0700, Tim Sprout <tm**@ptialaska.netwrote:
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
May 31 '07 #10
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 30 May 2007 20:54:54 -0700, Tim Sprout <tm**@ptialaska.netwrote:
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
May 31 '07 #11
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 :-)

--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
May 31 '07 #12
"Mihai N." <nm**************@yahoo.comwrote in message
news:Xn*******************@207.46.248.16...
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 :-)
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.
May 31 '07 #13
On Thu, 31 May 2007 00:33:57 -0700, Mihai N. <nm**************@yahoo.com
wrote:
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
May 31 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ricardo Jesus | last post by:
I'm reading an XML document and creating a CMS Posting for eacho specific node but i'm trying to save the posting using the title as Posting.Name. For this to work i have to remove all special...
1
by: Lala | last post by:
Hi, I need to check if a string contains unicode characters, and generate an alert if it does. (I do not want to parse the string to unicode). Anyone who knows how to do this? Thanks! :D
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
5
by: Joe Abou Jaoude | last post by:
Hi, in my code, i need to load a string in an xml document. However this string contains special characters like &,>,<," ... I need to replace these characters by &amp; &lt; ... however this is not...
4
by: smartic | last post by:
how to test string whether it contains special characters -------------------------------------------------------------------------------- please help dear experts on how to test string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.