如何解析具有多个小数点的字符串 [英] How to Parse a String with Multiple Decimal Points
问题描述
我想将一个字符串(例如"10.0.20")解析为一个数字,以便在C#.net中比较另一个具有相同格式的字符串
I was wanting to parse a string such as "10.0.20" into a number in order to compare another string with the same format in C#.net
例如,我将比较这两个数字以查看哪个小于另一个: 如果(10.0.30< 10.0.30)....
For example I would be comparing these two numbers to see which is lesser than the other: if (10.0.30 < 10.0.30) ....
我不确定应该为此使用哪种解析方法,因为在这种情况下,decimal.Parse(string)无法正常工作.
I am not sure which parsing method I should use for this, as decimal.Parse(string) didn't work in this case.
感谢您的时间.
@Romoku回答了我的问题,我不知道有一个Version类,这正是我所需要的.好吧,TIL.谢谢大家,如果不是您需要的话,我会花很多时间来研究表格.
@Romoku answered my question I never knew there was a Version class, it's exactly what I needed. Well TIL. Thanks everyone, I would have spent hours digging through forms if it wasn't for you lot.
推荐答案
The the string you're trying to parse looks like a verson, so try using the Version
class.
var prevVersion = Version.Parse("10.0.20");
var currentVersion = Version.Parse("10.0.30");
var result = prevVersion < currentVersion;
Console.WriteLine(result); // true
这篇关于如何解析具有多个小数点的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!