3
votes

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))

1
IIRC there is an ansistrings or ansistrutils unit that contains the functions for ansi strings. (No Delphi on this machine) - Marjan Venema
As @Marjan says, there it is. - TLama
The question that you should be asking yourself, is why you even want to compare two AnsiString variables. - David Heffernan
@DavidHeffernan: I have some reasons. Maybe we can discuss that in chat? (AnsiStrings in Delphi room) - Kromster

1 Answers

6
votes

Indeed, there's a AnsiStrings module that keeps all the older AnsStrings versions of string utils :)

So the solution is as follows:

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