Create a function like IsDigit()

S

skneife

Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);

Sam
 
T

Tobias Schröer

Hi,

Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);

Sam

do you mean digit like '0', '1', .. '9', which is a (or better nine)
simple equal operations, or number? If, which numbers? int, double?

Sounds like homework ;)

Tobi
 
C

Chris Shepherd

Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);

Use a regular expression. Something simple like "[0-9]+" should work.

Chris.
 
C

Calum

You can use int.TryParse or check that each character in the strign is
a digit:

foreach(char c in string s)
if !char.IsDigit(c)
return false

return true;

Calum.
 
M

Marc Gravell

Do you mean:
if(!char.IsDigit(c)) return false;

[which might actually answer the OP on its own]

Marc
 
J

Jesse Houwing

Hello Chris,
Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);
Use a regular expression. Something simple like "[0-9]+" should work.

Chris.

int val = 0;
return int.TryParse(stringInput, NumberStyles.Integer, val);

should work (though I don't have intellisense in my newsreader ;)) and should
be much faster than a regex.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What about:

bool MyIsDigit(string s)
{
return IsDigit(s);
}


:)
 
M

Michael Starberg

Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);

Sam

Sounds like 101 lesson 1.
We should not do your homework for you.
If this is a problem for you, you really need 'Programming for Dummies'

- Michael Starberg
 
R

rossum

Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);

Use a regular expression. Something simple like "[0-9]+" should work.

Chris.
<mode="pedant">
That does not work for all languages using non-Roman alphabets. For
example Arabic (?????), Unicode U+0660 on, or devanagari (?????),
Unicode U+0966 on, have their own sets of digits. The question is not
specified sufficiently clearly to determine if your answer is
sufficient or not.
</mode>

rossum
 
C

Chris Shepherd

rossum said:
<mode="pedant">
That does not work for all languages using non-Roman alphabets. For
example Arabic (?????), Unicode U+0660 on, or devanagari (?????),
Unicode U+0966 on, have their own sets of digits. The question is not
specified sufficiently clearly to determine if your answer is
sufficient or not.
</mode>

LOL@<mode="pedant" />, but I hardly call that being pedantic. It's very true, if
the OP is parsing non-Arabic numbering, it will be an issue.

Chris.
 
M

Michael Starberg

Chris Shepherd said:
LOL@<mode="pedant" />, but I hardly call that being pedantic. It's very
true, if the OP is parsing non-Arabic numbering, it will be an issue.

Chris.

I am not sure what you laughed about, but for me it was the non-valid xml.

<element attribute="value" />

Rossum is a sloppy pedant =)

- Michael Starberg
 
P

Peter Duniho

I am not sure what you laughed about, but for me it was the non-valid
xml.

How do you know it was supposed to be XML?

Maybe he's just using a markup that happens to be similar to XML, but
which has its own rules. For all you know, the markup he used could be
perfectly valid when interpreted correctly.
 
M

Michael Starberg

Peter Duniho said:
How do you know it was supposed to be XML?

Maybe he's just using a markup that happens to be similar to XML, but
which has its own rules. For all you know, the markup he used could be
perfectly valid when interpreted correctly.

Maybe...
 
L

Lasse Vågsæther Karlsen

rossum said:
Doeas any know the simplest way for create this type of function:
bool result = IsDigit(string s);
Use a regular expression. Something simple like "[0-9]+" should work.

Chris.
<mode="pedant">
That does not work for all languages using non-Roman alphabets. For
example Arabic (?????), Unicode U+0660 on, or devanagari (?????),
Unicode U+0966 on, have their own sets of digits. The question is not
specified sufficiently clearly to determine if your answer is
sufficient or not.
</mode>

rossum

Additionally, a "digit" is one digit, not a sequence of them, that's a
number.

I'd say the OP is either looking for a homework answer, or he's looking
for Int32.TryParse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top