Create a function like IsDigit()

  • Thread starter Thread starter skneife
  • Start date Start date
S

skneife

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

Sam
 
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
 
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.
 
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.
 
Do you mean:
if(!char.IsDigit(c)) return false;

[which might actually answer the OP on its own]

Marc
 
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.
 
Hi,

What about:

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


:-)
 
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
 
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
 
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.
 
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
 
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.
 
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...
 
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.
 
Back
Top