Determine if string contains only integer values - C# code please

Posted on 2012-09-12
Last Modified: 2012-09-12
Please provide working tested code that will determine if string contains only integer values - C# code please.  Spaces allowed.  Leading 0 allowed.

These would Pass:

note:  "_" represents a space

Question by:knowlton
    LVL 49

    Accepted Solution

    This should do it
    using System;
    using System.Text.RegularExpressions;
    namespace test
        class Program
            static void Main(string[] args)
                string input = " 0 1294 55678 ";
                if (Regex.Replace(input, "[^0-9 ]", "") == input)
                    Console.WriteLine("This one is good");

    Open in new window

    LVL 5

    Author Closing Comment

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Using int.TryParse(), after removing the spaces:
                string strValue = " 0 1294 55678 ";
                int value;
                if (int.TryParse(strValue.Replace(" ", ""), out value))

    Open in new window

    LVL 5

    Author Comment

    Thank you for the follow-up.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now