473,395 Members | 1,460 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,395 software developers and data experts.

C# equivalent of PHP's intval()

intval() in PHP is a simple function... you toss it a string and it
gives you the integer value. If the string is all text, the value is
0. It's very nice for simple form validation where you want to force
a form value to be an integer. If the user enters one, it gets used.
If they mess around, the value becomes 0. Simple.

So, after googling for how to do this in C# with asp.net, the best I
can come up with is Convert.ToInt32() which tosses an error if the
string you feed it isn't an integer. I don't want an error. I want a
zero.

Is there a function built into C#/asp.net that's like intval()?

Sep 10 '07 #1
5 6524
It would take you about three lines to return a zero from the function you
can write to do this. I dont believe theres a method out of the box for it.

Take a look at int32.parse also.

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
"Doesn't Work At McDonalds" <bu*******@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
intval() in PHP is a simple function... you toss it a string and it
gives you the integer value. If the string is all text, the value is
0. It's very nice for simple form validation where you want to force
a form value to be an integer. If the user enters one, it gets used.
If they mess around, the value becomes 0. Simple.

So, after googling for how to do this in C# with asp.net, the best I
can come up with is Convert.ToInt32() which tosses an error if the
string you feed it isn't an integer. I don't want an error. I want a
zero.

Is there a function built into C#/asp.net that's like intval()?

Sep 10 '07 #2
On Sep 10, 3:47 pm, "John Timney \(MVP\)"
<x_j...@timney.eclipse.co.ukwrote:
It would take you about three lines to return a zero from the function you
can write to do this. I dont believe theres a method out of the box for it.

Take a look at int32.parse also.

Regards

John Timney (MVP)http://www.johntimney.comhttp://www.johntimney.com/blog
John,

Thanks.

I wrote a quick function that uses a regex to check whether there are
any characters in the string (^[0-9]+$). If it fails the regex, it
returns 0. If not, it returns the numerical value of the string.

Sep 10 '07 #3

"Doesn't Work At McDonalds" <bu*******@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
intval() in PHP is a simple function... you toss it a string and it
gives you the integer value. If the string is all text, the value is
0. It's very nice for simple form validation where you want to force
a form value to be an integer. If the user enters one, it gets used.
If they mess around, the value becomes 0. Simple.

So, after googling for how to do this in C# with asp.net, the best I
can come up with is Convert.ToInt32() which tosses an error if the
string you feed it isn't an integer. I don't want an error. I want a
zero.

Is there a function built into C#/asp.net that's like intval()?
string s = "ABC";
int intval;
int.TryParse(s, out intval);
Sep 11 '07 #4
On Sep 10, 11:59 pm, Doesn't Work At McDonalds <burger...@gmail.com>
wrote:
I wrote a quick function that uses a regex to check whether there are
any characters in the string (^[0-9]+$). If it fails the regex, it
returns 0. If not, it returns the numerical value of the string.
How does it work with:

999999999999999999999999999999999999999

?

Ben's answer (TryParse) is the best one here.

Jon

Sep 11 '07 #5
Thanks to Ben for the awesome answer and to Jon for weighing in behind
it. Works great!

On Sep 11, 12:17 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
Ben's answer (TryParse) is the best one here.

Jon

Sep 11 '07 #6

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

Similar topics

1
by: Erich Trowbridge | last post by:
Has anybody seen this tool? It is awesome. check out http://vw.vermeer.org/ . It's a php front end for large-scale syslog deployments. It makes managing syslog in large networks a snap. The idea...
29
by: rjames.clarke | last post by:
I am developing an online application and the last thing I need to get a handle on is security. This app is very heavy with forms. Business critical data will be entered via forms and inserted in...
0
by: titecs | last post by:
Hi How I to send downstairs code to server ın the form of multı ımages and How I dısplay this form ın the web resim(turkish)=picture(english) one picture send picture_add.php
2
by: Sireen | last post by:
Hello everybody, am New in PHP. I would appreciate some help in the below code . What i need is a yearly calender that displays only the months without days .So i can load the data on a monthly...
10
by: chimambo | last post by:
Hi All, I have a little problem. I am retrieving records from a table and I want to update the records using checkboxes. I am able to display the database record quite alright and I have created...
10
by: phopman | last post by:
Hi there! Thanks for all the help so far. When my boss said I should be up to date as soon as possible, he meant last week. So I got a good rollicking when I came to work. Nice way to start my...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
11
by: sshade25 | last post by:
I am trying to insert some data into my postgresql database table using an html form and a php script. The problem here is that when the script is run, it does not insert data into the last two...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.