Search code examples
delphiunicodestring-comparison

How to use SameText for AnsiStrings in modern Delphi?


I need to compare two AnsiStrings to see if they have the same text (ignoring case).

var
  text1, text2: AnsiString;
begin
  if SameText(text1, text2) then
  ...

In pre-Unicode Delphi I would use SameText function, but in modern Delphi there is only Unicode version of it, so I'm getting this warning: W1057 Implicit string cast from 'AnsiString' to 'string' upon call.

My question is, how to properly compare AnsiStrings in modern Delphi without getting compiler warnings (and without superfluously having to cast both strings to UnicodeString(text))


Solution

  • Indeed, there's an AnsiStrings module that keeps all the older AnsiString versions of string utils :)

    So the solution is as follows:

    uses
      System.AnsiStrings;
    ...
    var
      text1, text2: AnsiString;
    begin
      if AnsiStrings.SameText(text1, text2) then
      ...