Xor-el
Xor-el

Reputation: 67

equivalent enumerable.max function in delphi

does Delphi have any equivalent of the csharp code alphabet.max where alphabet is a string variable or is there an equivalent function?

I am trying to port the following code from csharp to delphi.

string alphabet = "ABCD";
invalphabet = new int[alphabet.Max() + 1];

https://msdn.microsoft.com/en-us/library/bb347632(v=vs.90).aspx

thanks

Upvotes: 2

Views: 322

Answers (1)

David Heffernan
David Heffernan

Reputation: 613282

You are probably looking for something like this:

uses
  Math; // for the Max function
....
var
  i: Integer;
  invalphabet: array of Integer;
  maxOrdinal: Integer;
....
maxOrdinal := -1;
for i := 1 to Length(alphabet) do
  maxOrdinal := Max(maxOrdinal, ord(alphabet[i]));
if maxOrdinal = -1 then
  // handle error condition
SetLength(invalphabet, maxOrdinal + 1);

Be alive to possible encoding mismatches. The C# code uses UTF-16, and the Delphi code uses either UTF-16 or ANSI depending on your Delphi version. Of course, you may supply an alphabet that is restricted to ASCII.

Upvotes: 4

Related Questions