473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to determinatel if a string is a number

ad
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?
Nov 17 '05 #1
5 5202
ad,

You can use the static TryParse method on the Double structure. This
will allow you to try and determine if the string contains a number.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?

Nov 17 '05 #2
ad wrote:
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?


I can imagine two useful ways. First, you could use the Int32.Parse
method to try the conversion. If it throws an exception, you know that
there's something wrong with the string:

...
try {
int val = int.Parse(myString);
}
catch (Exception e) {
// not an integer

// Note: it might be nicer to catch the three exceptions that are
// documented to be thrown by the Parse method.
}
...

The same approach can be coded a lot cleaner in .NET 2, where the
TryParse method has been added. Like this:

...
int result;
if (int.TryParse(myString, out result))
// now result has a valid value
And third, most generally, you can simply analyze the string to see what
it contains. This approach is obviously more flexible than the others,
because you can use it for more complicated strings as well. A regular
expression is good for this kind of check:

...
if (Regex.Match(myString, "^\d+$").Success)
// now you know that the string has only digits
Hope this helps!
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3
If you're using .NET 2.0 use the TryParse method of Double/Int etc.

If you're using .NET 1.1 you could use the int.Parse method and wrap it in a
try catch or use a regex to determine if it is a number.

An example regex:

string inputString = "1034";
bool isNumber = Regex.IsMatch(inputString, @"^\d+$");

If you want to allow for a decimal point then use a regex like
@"^\d*(\.\d+)?$"

Hope this helps.

--
Brian Delahunty
Ireland

http://briandela.com/blog
"ad" wrote:
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?

Nov 17 '05 #4
ad
I use
if (Int16.TryParse(myString,Int16))
{....}

But can't compile.
Could some one give me an example?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> ¼¶¼g©ó¶l¥ó·s»D:uU**************@tk2msftngp13.phx.g bl...
ad,

You can use the static TryParse method on the Double structure. This
will allow you to try and determine if the string contains a number.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?


Nov 17 '05 #5
ad,

You need to use it on the double structure, like so:

if (double.TryParse(myString, ref myDouble))

The TryParse method was not added to the Int16 structure until .NET 2.0.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I use
if (Int16.TryParse(myString,Int16))
{....}

But can't compile.
Could some one give me an example?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
¼¶¼g©ó¶l¥ó·s»D:uU**************@tk2msftngp13.phx.g bl...
ad,

You can use the static TryParse method on the Double structure. This
will allow you to try and determine if the string contains a number.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
I want user input a string to a text box, like "101",
How can I determinate if the string that a user input is composite of
digital?



Nov 17 '05 #6

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

Similar topics

8
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the...
16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
6
by: Tony D. Abel | last post by:
I have the following string format that I am trying to parse 1-1- 1-1- 1-1-1 2-222- 22-2-3 or
25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.