Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a string is greater than another string

Tags:

string

c#

integer

I have two strings

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

I need to evaluate somehow that B is greater than A (version wise) either converting those two strings to integers or decimals or something.

I tried the following

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

but keep getting the following error, "Input string was not in a correct format."

Any help will be appreciated.

like image 535
Butters Avatar asked Nov 28 '22 21:11

Butters


1 Answers

See the Version Class.

You're able to 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 haven't personally tested this exact scenario, but the Version class allows you to use comparison operators like I've shown here.

like image 172
tnw Avatar answered Dec 04 '22 12:12

tnw