字符串转换为int和试验的成功在C# [英] Convert string to int and test success in C#

查看:161
本文介绍了字符串转换为int和试验的成功在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何检查是否字符串转换 INT?

让我们说我们有像家的数据,50,狗,45.99,我想知道我是否应该只使用字符串或使用解析 INT 值,而不是。

Let's say we have data like "House", "50", "Dog", "45.99", I want to know whether I should just use the string or use the parsed int value instead.

在JavaScript中,我们有这个 parseInt函数()函数。如果字符串的无法解析,它还是会回到为NaN

In JavaScript we had this parseInt() function. If the string couldn't be parsed, it would get back NaN.

推荐答案

Int32.TryParse(字符串,Int32)将 - <一个href=\"http://msdn.microsoft.com/en-us/library/f02979c7.aspx\">http://msdn.microsoft.com/en-us/library/f02979c7.aspx

  bool result = Int32.TryParse(value, out number);
  if (result)
  {
     Console.WriteLine("Converted '{0}' to {1}.", value, number);         
  }

这篇关于字符串转换为int和试验的成功在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆