检查字符串是否是使用C#的有效年份的最佳方法是什么?
我目前有一个包含{'All','2009','2008'}等值的下拉列表,我想知道选择是其中一个日期还是'全部'字段.
目前我正在检查 bool isYearValid = (Year.ToLower() == "all") ? false : true;
如何检查该值是否为有效年份,以便我不必对"全部"进行硬编码检查?
目前我有一个看起来像12.53467345的双..现在我想删除点后的数字,所以我得到"12",我怎么能这样做?我想用TryParse,但不太懂得怎么做.
谢谢!
是否可以使用Int32.TryParse?检查负数?
int defaultTop;
displayTop = (Int32.TryParse(DisplayTop, out defaultTop) ? Convert.ToInt32(DisplayTop) : 1000 );
Run Code Online (Sandbox Code Playgroud) I have strange situation, when I use API. I get object in JSON, which I deserialize. This object contains string property which is parsed to decimal.
To do this I use this code. I live in Poland where decimal separator is ',', so I use replace method.
string input ="160.00"; //value from API
decimal result;
decimal.TryParse(input.Replace('.',','), out result);
Run Code Online (Sandbox Code Playgroud)
From time to time I result is equals 16000!! (I suppose TryParse method delete separator, it not determined). How can I prevent …
为什么此代码收到"8:16 AM"之类的输入错误:
string time = Console.ReadLine();
DateTime outValue = DateTime.MinValue;
bool error = DateTime.TryParseExact(time, "HH:mmtt" /*"hh:mmtt"*/, CultureInfo.InvariantCulture, DateTimeStyles.None, out outValue);
Console.WriteLine(error);
Console.WriteLine(outValue);
Console.Read();
Run Code Online (Sandbox Code Playgroud)
我应该设置什么才能接受这样的输入"8:16"并将其转换为DateTime对象?
我有以下代码
public static void Main()
{
DateTime D = new DateTime();
D = DateTime.Now;
string s1 = D.ToString("ddMMMMyyyy");
Console.WriteLine(s1);
Console.WriteLine(DateTime.TryParseExact(s1, "ddMMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out D));
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我意识到我正在犯的明显错误.
我转换DateTime.Now为自定义格式的字符串并尝试将其转换回来,但TryParseExact返回false.
我正在阅读C#计算机编程基础知识
string str = Console.ReadLine();
int Value;
bool parseSuccess = Int32.TryParse(str, out Value);
Console.WriteLine(parseSuccess ? "The square of the number is " + (Value * Value) + " . " : "Invalid number!" );
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,在第三行bool parseSuccess = Int32.TryParse(str, out Value);,Int32.TryParse()它不会返回int值吗?怎么会这样bool?这个关键字到底out意味着什么?
我花了一天的时间来弄清楚if语句之一为字符串值返回true的问题.
我们正在解析以检查值是数字还是字符串.我发现使用了这个语句,当字符串值作为6E02时,语句返回true,这是一个double值.
var double temp;
var val ="6E02"
result = double.TryParse(val, out temp)
Run Code Online (Sandbox Code Playgroud)
如何修复此问题以返回字符串的结果为false,如(Number)E0(Number)
简单的方法我相信首先检查文本是否包含E0,如果它只返回false.但有没有更好的方法来处理这个或另一个内置的方法来替换方法?