Determine if a row is larger than another row

I have two lines

string A = "1.0.0.0"; string B = "1.0.0.1"; 

I need to somehow evaluate that B is greater than A (version wise), either by converting the two lines to integers, or decimal numbers, or something like that.

I tried the following

 Decimal S = Convert.ToDecimal(A); int S = Convert.ToInt32(A); 

but keep getting the following error: "The input string is not in the correct format."

Any help would be appreciated.

+4
source share
6 answers

See Version Class .

You can do something like this:

 Version a = new Version("1.0.0.0"); Version b = new Version("1.0.0.1"); if (b>a) //evaluates to true blah blah blah 

I personally have not tested this exact scenario, but the Version class allows you to use comparison operators, as shown here.

+20
source

If your string has at most 4 numeric parts (separated by a character . ), You can use the Version class to get a strongly typed object that matches these strings. Version implements various comparison operators ( == , > , < , etc.) in the expected mode, so you can find out more:

 var a = new Version(A); var b = new Version(B); if(a > b) // a is larger else if (a < b) // b is larger else // they are identical 

If there are more than 4 parts, you need to divide each line into numeric components, convert them to the numerical equivalent and compare the two resulting collections.

Sort of:

 var aParts = A.Split('.'); var bParts = B.Split('.'); // assumes the string have the same number of parts for(int i = 0; i < aParts.Length; i++) { var currA = int.Parse(aParts[i]); var currB = int.Parse(bParts[i]); if(currA == currB) continue; if(currA > currB) // A is greater than B else // B is greater than A } 
+3
source

You can take a look at the System.Version class. If the strings are in this format or can be converted to version.

The version has compactors

+3
source

If you want to compare version strings in .NET, you can use the Version class.

 Version version = new Version("1.0.0.0"); Version otherVersion = new Version(""1.0.0.1"); 

The class provides operators with the ability to check whether a version is larger or smaller than another.

+1
source

Divide by ".". Then convert each part to int. Starting from the left: if fragment A is below, report that A is the first. If fragment B is below, report that B is the first. Otherwise, skip to the next snippet. If you are already at the last fragment, let them know that they are equal.

If your lines have no more than four parts (for example, version numbers), then, like others, it was suggested to use the System.Version class.

+1
source

Instead of using VersionClass, a quick approach would be something like this.

 string A = "1.0.0.0"; string B = "1.0.0.1"; int versionA = Convert.ToInt32(A.Replace(".", string.Empty)); int versionB = Convert.ToInt32(B.Replace(".", string.Empty)); if (b>a) //something will happen here 

Replace changes the first line to the second in this case string.Empty is equal to "".

-2
source

All Articles