Author Topic: (C#) IsNumeric  (Read 6099 times)

0 Members and 1 Guest are viewing this topic.

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
(C#) IsNumeric
« on: August 03, 2012, 01:48:11 am »
do i realy need to explain ?
Code: (c#) [Select]
        public bool IsNumeric(string str)
        {
            foreach (Char c in str)
            {
                if (!Char.IsNumber(c))
                {
                    return false;
                }
            }
            return true;
        }
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: (C#) IsNumeric
« Reply #1 on: August 03, 2012, 04:15:08 am »
I don't want to sound rude or anything, but why? A quick Google search would yeild the same thing,  or a we quick glance of the doc's.

Either way, I've noticed a lot more activity from you. Nice.
>>>import this
-----------------------------

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
Re: (C#) IsNumeric
« Reply #2 on: August 03, 2012, 06:52:35 am »
I don't want to sound rude or anything, but why? A quick Google search would yeild the same thing,  or a we quick glance of the doc's.

Either way, I've noticed a lot more activity from you. Nice.

you would think so , but thats the thing there is no inbuilt IsNumeric function in C#
that surprised me to, thats why i posted the function
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: (C#) IsNumeric
« Reply #3 on: August 03, 2012, 10:19:29 am »
seriously? no built-in function like that? :D lol, Microsoft...

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: (C#) IsNumeric
« Reply #4 on: August 03, 2012, 04:57:33 pm »
There's no isNumeric function in Java either for the String class. You can use Integer.parseInt which will throw an exception if the string is not a number but that's a really bad use of exceptions. Added a function just like the on Bubzuru posted in my utility class last week.

P.s. There is a isNumeric() function the Apache StringUtils library.
Satan911
Evilzone Network Administrator

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: (C#) IsNumeric
« Reply #5 on: August 04, 2012, 09:39:10 am »
Your method only works for integers. You should call it isInteger() instead of isNumeric(), otherwise a String like "1.3" will be considered as non-numeric.

Offline bubzuru

  • Knight
  • **
  • Posts: 395
  • Cookies: 21
  • everything is contained in the data
    • View Profile
    • New School Tools
Re: (C#) IsNumeric
« Reply #6 on: August 04, 2012, 11:15:21 pm »
Your method only works for integers. You should call it isInteger() instead of isNumeric(), otherwise a String like "1.3" will be considered as non-numeric.

^^ that is true , this function does not work for floating point numbers.
if anybody needs a workaround please post here 
Damm it feels good to be gangsta
http://bubzuru.comule.com

Offline fruitcake2212

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 1
    • View Profile
Re: (C#) IsNumeric
« Reply #7 on: August 08, 2012, 11:07:26 am »
There's no isNumeric function in Java either for the String class. You can use Integer.parseInt which will throw an exception if the string is not a number but that's a really bad use of exceptions.

You can use the TryParse method instead, it will not throw exception.

Also, you could use the IsNumeric function of VisualBasic in your C# application. You would need to reference the visualbasic dll in your project
« Last Edit: August 08, 2012, 11:10:10 am by fruitcake2212 »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: (C#) IsNumeric
« Reply #8 on: August 08, 2012, 12:41:24 pm »
TryParse is a C# method, not Java.

Offline fruitcake2212

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 1
    • View Profile
Re: (C#) IsNumeric
« Reply #9 on: August 08, 2012, 12:59:55 pm »
TryParse is a C# method, not Java.

Yes sorry I've read Satan911's answer too fast. My answer are possibilities for C#
« Last Edit: August 08, 2012, 01:00:37 pm by fruitcake2212 »