How can I convert String to Int in C#?
2014-06-10 23:23
453 查看
http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int
Try this:
or better yet:
Also, since
a
can its return value to make decisions about the results of the parsing attempt:
If you are curious, the difference between
best summed up like this:
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. - MSDN
Try this:
int x = Int32.Parse(TextBoxD1.Text);
or better yet:
int x = 0; Int32.TryParse(TextBoxD1.Text, out x);
Also, since
Int32.TryParsereturns
a
boolyou
can its return value to make decisions about the results of the parsing attempt:
int x = 0; if (Int32.TryParse(TextBoxD1.Text, out x)) { // you know that the parsing attempt // was successful }
If you are curious, the difference between
Parseand
TryParseis
best summed up like this:
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. - MSDN
相关文章推荐
- How to convert from int to string in objective c
- How to convert from int to string in objective c
- Different ways how to escape an XML string in C#
- 如何把string解析为int?[C#] How to Parse a string to an int? [C#]
- How to convert an IPv4 address into a integer in C#?
- How Do I Convert an Integer to a String in Excel VBA
- How to convert std::string to LPCWSTR in C++ (Unicode)
- How to convert from string to datetime in sql server? - Sealyu - BlogJava
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C#.
- How to convert int to string with C++ - forest的日志 - 网易博客
- Convert string to int in C++
- C# How to Convert String to ASCII OR how to convert ASCII To String
- How to convert XML to JSON in ASP.NET C#
- Different ways how to escape an XML string in C# (zz)
- How can I convert printStackTrace message to a string?
- How to import dll in c# that can be accessed by [DllImport]
- In Java, how do I read/convert an InputStream to a String? - Stack Overflow
- How to convert char array to String in Java
- How to convert List<Integer> to int[] in Java?
- Easiest way to convert int to string in C++