本文整理汇总了C#中System.Int16.TryParse方法的典型用法代码示例。如果您正苦于以下问题:C# Int16.TryParse方法的具体用法?C# Int16.TryParse怎么用?C# Int16.TryParse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Int16.TryParse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("16051");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
short number;
bool result = Int16.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:32,代码来源:Int16.TryParse 输出:
Attempted conversion of '' failed.
Converted '16051' to 16051.
Attempted conversion of '9432.0' failed.
Attempted conversion of '16,667' failed.
Converted ' -322 ' to -322.
Converted '+4302' to 4302.
Attempted conversion of '(100)' failed.
Attempted conversion of '01FA' failed.
示例2: Main
//引入命名空间
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "10677";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
short number;
bool result = Int16.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
else
Console.WriteLine("Attempted conversion of '{0}' failed.",
Convert.ToString(stringToConvert));
}
}
开发者ID:.NET开发者,项目名称:System,代码行数:57,代码来源:Int16.TryParse 输出:
Converted '10677' to 10677.
Attempted conversion of '-30677' failed.
Converted '10345.00' to 10345.
Attempted conversion of '10345.72' failed.
Converted '22,593' to 22593.
Attempted conversion of '12E-01' failed.
Converted '12E03' to 12000.
Converted '80c1' to -32575.
Attempted conversion of '0x80C1' failed.
注:本文中的System.Int16.TryParse方法示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论