简体   繁体   中英

Text comparison greater than

I have text/lines like below and my requirement is check if one is greater than other. Is it really possible using c#? I you see the text is already sorted.

  • 9012345EMA1234
  • 9012345GHA1234
  • 9012345KMA1234
  • 9012345LHA1234
  • 9012345LHE1234
  • 9012345PHA1234

I appreciate anyone response.

This link might help.

var something = String.Compare("9012345EMA1234", "9012345LHE1234");

This will give you a -1, which means that the 2nd string is the greater of the 2.

var something = String.Compare("9012345LHE1234", "9012345EMA1234");

This will give a 1....which means that the 1st string is the greater of the 2.

A 0 means they are equal.

Also try this, put all your strings in a List and then:

    List<string> strings = new List<string>() { "9012345EMA1234", "9012345LHE1234", "9012345KMA1234" };

    string answer = strings.Aggregate((a, b) => String.Compare(a, b) == 1 ? a : b);

This will get you the "greatest" string.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM