473,973 Members | 7,780 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there any IsNumeric in C#?

Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(stri ng)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Nov 19 '05 #1
5 1624
Import Microsoft.Visua lBasic and use it!

if (Microsoft.Visu alBasic.Informa tion.IsNumeric( "5"))
{
//Do Something
}

if (Microsoft.Visu alBasic.Informa tion.IsNumeric( "yadda"))
{
//Do Something
}

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
=============== =============== ========
"Steve Kershaw" <st***********@ yahoo.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(stri ng)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Nov 19 '05 #2
Steve,

Try this:

public static bool IsInteger(strin g theValue)
{
try
{
Convert.ToInt32 (theValue);
return true;
}
catch
{
return false;
}
}
}

"Steve Kershaw" wrote:
Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(stri ng)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Nov 19 '05 #3
From Scott Hanselman's blog at :

http://www.hanselman.com/blog/Explor...mericForC.aspx

Actually, this doesn't really do what IsNumeric does,
as IsNumeric should also return true for floating point numbers.

That code does not return true for floating point numbers,
which -nevertheless- *are* numbers.

That is really more of an "IsInt".

Importing Microsoft.Visua lBasic does the complete job.


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
=============== =============== ========
"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:D7******** *************** ***********@mic rosoft.com...
Steve,

Try this:

public static bool IsInteger(strin g theValue)
{
try
{
Convert.ToInt32 (theValue);
return true;
}
catch
{
return false;
}
}
}
"Steve Kershaw" wrote:
Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(stri ng)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Nov 19 '05 #4
In the .Net 2.0 CLR, there is an Int32.TryParse( ) method that doesn't throw
an exception, and you can use that. In .Net 1.1, you're pretty much stuck
with the alternatives that the others have already proposed.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Steve Kershaw" <st***********@ yahoo.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(stri ng)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Nov 19 '05 #5
There is no equivalent for this function in C#. VB.NET has lots of
functions that C# developers have to create manually.
Or, you can import the visual basic namespace and use its IsNumeric function
from within C#.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Steve Kershaw" <st***********@ yahoo.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(stri ng)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Nov 19 '05 #6

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

Similar topics

8
2336
by: eje | last post by:
IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the following error message: "Input string was not in a correct format." I use the following function in a validating class to use when needed. (Value = H880118A gave the error (like other 'unconvertible' strings)) Public Function Numeric(ByVal value) As Boolean
4
13282
by: Eugene Anthony | last post by:
I have received the following feedback for the two functions bellow: "The ISNUMERIC test is WORTHLESS for checking for an INT value, because ISNUMERIC will happily accept DOUBLE values, such as 89.11998811777 and other values that are simply *NOT* INT values." <% function isZip(input)
14
40167
by: Kenny | last post by:
Hello, I would like to know if the function IsNumeric requires a header like #include <iostream> to be functionnal thanks ken
8
2622
by: John Bowman | last post by:
Hello, Does anyone have a good/reliable approach to implementing an IsNumeric() method that accepts a string that may represent a numerical value (eg. such as some text retrieved from an XML file)? Thus if you pass it "1" or "5.12345" it returns true, but "1b3q" it returns false? I know VB has this sort of function, but was wondering how to implement something similar in C#. I suppose I could use the Convert methods and catch any...
3
1774
by: martin | last post by:
Hi, is there a dotnet function (other than the old isnumeric from VB) to check whether an object is numeric or not. also I notice that all the old vb functions such as split / isnumeric / ubound etc are still availible by default in any VB.net web application. is this because the vb library is being imported?? would it be best to totally remove this library (assuming that it is being imported - which I can't see in my references) so...
7
2393
by: Nathan Truhan | last post by:
All, I think I may have uncovered a bug in the IsNumeric function, or at least a misunderstanding on functionality. I am writing a Schedule Of Classes Application for our campus and have a section that lists the Times, Buildings and rooms for courses. In this section I have a function called PadCell that takes3 parameters, one the value, one a padd count and one a a boolean input called StripNumeric, that if true, checks if the value...
8
2595
by: moondaddy | last post by:
What's the .net framework equivalent of the vb function isnumeric? If there isn't one, how can I test a string variable to see if its a number or not? I don't want to use isnumeric if possible -- moondaddy@noemail.noemail
12
3078
by: sck10 | last post by:
Hello, I am trying to determine if a value is NOT numeric in C#. How do you test for "Not IsNumeric"? protected void fvFunding_ItemInserting_Validate(object sender, FormViewInsertEventArgs e) if (e.NewValues != "" && Not IsNumeric(e.NewValues)) {
12
1681
by: Paul | last post by:
Hi, I am trying to check a string to see if it's first 3 characters are numeric and if they are, to replace those 3 characters with something else. I've tried this but nothing happens... newname = Replace(newname, VB.Left(newname, 3) = "101-", "101. ")
17
2332
by: MLH | last post by:
I have tested the following in immed window: ?isnumeric(1) True ?isnumeric(1.) True ?isnumeric(1.2) True ?isnumeric(1.2.2)
0
10160
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11399
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8453
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6543
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4726
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.