I am trying to get a the sub releases of a program, and this returns the following string Release 4.2.3 of programX
but also sometimes: Release 4.3 of programX
. I want to do a check if the string contains 3 or more decimal places (like 4.3.2).
I found This question with answer but it's not for C#, is this possible in C# and if so what would be the best approach?
Use linq.
bool more3dec = input.Count(c => c == '.') >= 2;
Or regex. (Checking only points within numeric part)
bool more3dec = Regex.IsMatch(input, @"\d+\.\d+\.\d+");
you can do this quite a view ways but if you want to know if it have 2 periods .
or not then you can do this for instance.
var versionStr = "Release 4.2.3";
var nuberDecimals = versionStr.Split('.').Length - 1;
nuberDecimals = 2;
var input = "Release 4.2.3 andSomeText.With.Dots.Or.Namespaces";
var isValid = Regex.IsMatch(input.Split(' ')[1], @"\d+\.\d+\.\d+");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.