?
Solved

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

Posted on 2012-09-12
4
Medium Priority
?
308 Views
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

"_01294"
"01294"
"0_1294"
"_0_1294"
"0_1294_55678"
"0_1294_55678_"
"_0_1294_55678_"
"_"
0
Comment
Question by:Tom Knowlton
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38392861
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");
            else
                Console.WriteLine("Nope");
        }
    }
}

Open in new window

0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 38392869
Thanks!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38392897
Using int.TryParse(), after removing the spaces:
            string strValue = " 0 1294 55678 ";
            int value;
            if (int.TryParse(strValue.Replace(" ", ""), out value))
            {
                Console.WriteLine("Valid");
            }
            else
            {
                Console.WriteLine("Invalid");
            }

Open in new window

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 38392972
Thank you for the follow-up.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

807 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