special characters

  • Thread starter Thread starter JustCoding
  • Start date Start date
J

JustCoding

In C# how can I check for non-numeric characters in a file?
example: if the file has !@#$%^ in it, how can I check for that and create
an error message for that file?
 
JustCoding,

Do you want non-numeric characters, or that range of characters? Either
way, you will have to read through the file character by character, and then
check to see if the character is a number. You can do this by getting the
character and then calling the IsNumeric method on the character. If it
returns false, you have an error.

If the file isn't too big, and you can load it into a string, you could
also use a regular expression to do this, searching for all characters that
aren't in the range you are looking for (something like the negative of
'[0-9]*' I think, I'm not that experienced with regular expressions). If
you get a match, it is an error.

If the file is particularly large though, loading the contents into a
string will be an issue, and you will see performance suffer.

Hope this helps.
 
I can't allow any character that is not a number or letter.
example: !@#$%^&* would be invalid


Nicholas Paldino said:
JustCoding,

Do you want non-numeric characters, or that range of characters?
Either way, you will have to read through the file character by character,
and then check to see if the character is a number. You can do this by
getting the character and then calling the IsNumeric method on the
character. If it returns false, you have an error.

If the file isn't too big, and you can load it into a string, you could
also use a regular expression to do this, searching for all characters
that aren't in the range you are looking for (something like the negative
of '[0-9]*' I think, I'm not that experienced with regular expressions).
If you get a match, it is an error.

If the file is particularly large though, loading the contents into a
string will be an issue, and you will see performance suffer.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

JustCoding said:
In C# how can I check for non-numeric characters in a file?
example: if the file has !@#$%^ in it, how can I check for that and
create an error message for that file?
 
Hi,

You could use char.IsLetterOrDigit( char )


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


JustCoding said:
I can't allow any character that is not a number or letter.
example: !@#$%^&* would be invalid


Nicholas Paldino said:
JustCoding,

Do you want non-numeric characters, or that range of characters?
Either way, you will have to read through the file character by
character, and then check to see if the character is a number. You can
do this by getting the character and then calling the IsNumeric method on
the character. If it returns false, you have an error.

If the file isn't too big, and you can load it into a string, you
could also use a regular expression to do this, searching for all
characters that aren't in the range you are looking for (something like
the negative of '[0-9]*' I think, I'm not that experienced with regular
expressions). If you get a match, it is an error.

If the file is particularly large though, loading the contents into a
string will be an issue, and you will see performance suffer.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

JustCoding said:
In C# how can I check for non-numeric characters in a file?
example: if the file has !@#$%^ in it, how can I check for that and
create an error message for that file?
 
Nicholas Paldino said:
Do you want non-numeric characters, or that range of characters? Either
way, you will have to read through the file character by character, and then
check to see if the character is a number. You can do this by getting the
character and then calling the IsNumeric method on the character. If it
returns false, you have an error.

"IsNumeric" is a VB function. Did you mean Char.IsNumber() ?
Althought, I think you'd want Char.IsDigit(), because IsNumber has a rather
loose definition of a number ("V" passes as it's the Roman number 5).

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top