Our services: SOFTWARE DEVELOPMENT,  WEB DEVELOPMENT, DATABASE DESIGN and ADMINISTRATION, ENHANCEMENT of EXISTING SOFTWARE, ENHANCEMENT of EXISTING WEB SITES, ENHANCEMENT of EXISTING DATABASE, GRAPHIC DESIGN – LOGOS, BANNERS, ICONS…, PHOTOGRAPHY: SHOOTING PHOTOS, IMPROVING AND RETOUCHING, VIDEO PRODUCTION: RECORDING VIDEOS, CREATE MOVIES, PUBLISHING, SEO – SEARCH ENGINES OPTIMISATION, SOCIAL NETWORKS – POSTS ON TWITTER, GOOGLE+, FACEBOOK, LINKEDIN.

           Search  Search  

How to test string for an Integer?
  More Programming ... 

  SoftFern.com on Twitter   SoftFern.com on YouTube   SoftFern.com on flickr   SoftFern.com on Pinterest
SoftFern.com on Facebook   SoftFern.com on Google+   SoftFern.com on Tumblr   SoftFern.com on LinkedIn



Our services: SOFTWARE DEVELOPMENT,  WEB DEVELOPMENT, DATABASE DESIGN and ADMINISTRATION, ENHANCEMENT of EXISTING SOFTWARE, ENHANCEMENT of EXISTING WEB SITES, ENHANCEMENT of EXISTING DATABASE, GRAPHIC DESIGN – LOGOS, BANNERS, ICONS…, PHOTOGRAPHY: SHOOTING PHOTOS, IMPROVING AND RETOUCHING, VIDEO PRODUCTION: RECORDING VIDEOS, CREATE MOVIES, PUBLISHING, SEO – SEARCH ENGINES OPTIMISATION, SOCIAL NETWORKS – POSTS ON TWITTER, GOOGLE+, FACEBOOK, LINKEDIN.


Free wallpapers of the hottest girls of the 2014 Winter Olympics

Complete guide for Euro 2012’, about 300 hundred pages filled with comprehensive information about Euro 2012


Euro 12 - teams, managers, players, fixtures, referees, the brand new ‘Tango 12’ Adidas ball and much, much more. Few hundreds amazing photos.


E-books, free e-books, Euro 2012 e-books


Auckland events in photos at bonanz.com


1 500 photos of fun-run 'Ports of Auckland Round the Bays 2012' at bonanz.com


Ocean swim - more than 1 500 photos of State King of the Bays

 

SoftFern.com Forums BonaNZ.com forums

SoftFern.com Forums - articles, news, links 

 

BonaNZ.com Forums - articles, news, links 


Photos of State King of the Bays event at bonanz.com


‘Complete guide for Euro 2012’: Given profile all 368 footballers with their photos.
 
 


‘Complete guide for Euro 2012’: Read about star players of each squad. Find out who is the key player of each team. What Pele expects of Euro 2012?



‘Complete guide for Euro 2012’: Compare annual salary of coaches participating in the Euro-2012 - and how it happened that the team of the most paid coach finished the last in their group?




‘Complete guide for Euro 2012’: Euro 2012 footballers: who are the most expensive players? Who are the Rising stars at Euro 2012?



E-books, free e-books, Euro 2012 e-books
 
  
 Popularity 
 Rating: 3.4      Rate this article   32 
 11 Aug 2011

How to test string for an Integer?

 







This is quite common task: to check whether some string represents an integer number or not. Using Parse method and catching an exception is quite costly from a performance point.

So use Int32.TryParse() method.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
________________________________________


public static bool TryParse(
string s,
out int result
)
Parameters
s : Type: System.String - A string containing a number to convert.
result : Type: System.Int32 - When this method returns, contains the 32-bit signed integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.
Return Value
Type: System.Boolean - true if s was converted successfully; otherwise, false.
________________________________________
The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.
This overload of the TryParse method interprets all digits in the s parameter as decimal digits. To parse the string representation of a hexadecimal number, call the Int32.TryParse(String, NumberStyles, IFormatProvider, Int32) overload.
Examples, how to check whether a string contains an integer or not.
________________________________________

private void Test()
{

CheckIsInteger("281");
CheckIsInteger("-547");
CheckIsInteger("sdsdsd");
CheckIsInteger("0ewew");
CheckIsInteger("589.0");
CheckIsInteger("02FA");
CheckIsInteger("+297");

}



private void CheckIsInteger(string value)
{
int number;
bool result = Int32.TryParse(value, out number);

if (result)
{
Console.WriteLine("The " + value + " is integer.");
}
else
{
Console.WriteLine("The " + value + " is not integer.");
}
}


The result is:

The 281 is integer.
The -547 is integer.
The sdsdsd is not integer.
The 0ewew is not integer.
The 589.0 is not integer.
The 02FA is not integer.
The +297 is integer.

The Int32.TryParse() method supported in such versions of .NET Framework:
4, 3.5, 3.0, 2.0


  
  More similar links:

How to test Credit Cards validity in C#. Sample code.

How to fix HTTP Error 404.3 - Not Found (aspx file not recognized by IIS)

Pointers Basics

Drag and drop a file in C# - easy.

How to open documents in Microsoft Visual Studio 2010 on the right of existing Tab.

Hardware Requirements for Microsoft Visual Studio 2010

Usage of Unsafe Code in C#

 
    
 Rate this article from 1 to 5 

      Comments
             
 

11.07.2012    Gabriel

Why do people write first conmmet? That's really lame. Anyway, another great vid. I feel like the pace you go at makes it much easier to learn than most of the other youtube tutorials I've seen, and the order in which you present the material makes b

              
        Add comment
Your name:
Your comment:
                      
 Tags: C#, SoftFern Tutorials, SoftFern free Tutorials and Article, SoftFern Windows and other Operatin, SoftFern Programming Tutorials, Parse, TryParse, .NET Framework, integer, test for integer, is integer, Int32, Int32.TryParse()
   More Programming ...
 


  Home page Weird We sell Programming Windows
  News Our Photos Photoshop and Photography SQL server Microsoft Office
  Tech News Our Portfolio iPad Misc Web sites development
  Videos Archive Androids Graphic design Software development
  Auckland and New Zealand About Us Tablet PCs CD/DVD presentation Play games online
  Football Contact us Round the Bays 2012 photos Ocean Swim 2012 photos Auckland Events
  Sport Our Portfolio Stock Images Stock Photos Links Exchange
  Hot Girls Our clients On Twitter Add link Site Map
  Our Twitters’ gadget Site Map Google+ Site Grid Poklonskaya.info
  Health and Beauty Site Grid Pinterest Natalia Poklonskaya.info Soft Fern forums
  Misc News Web pages Facebook Photos, images Hair & Beauty
  On YouTube CD/DVD presentaton Tumblr Daily Motion Flickr
  YouTube - 2 Graphics LinkedIn Live Journal Stumble Upon
  Vimeo - videos Tutorials - Videos Tutorials - Weird Most Popular on Internet Yahts, boats, Tall Ships

SoftFern (New Zealand LTD). 
Design and development by SoftFern 2003 - 2014 Graphics supplied by BonaNZ.com