Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi XE2 AnsiFormat() and ANSI String constants

Is there a handy Format() function that works only on Ansi strings? Because everytime I use an AnsiString with Format() I get a warning. And no, I don't want Delphi to convert my AnsiStrings back and forth between Wide and Ansi strings. That is just making things awfully slower. Also, is there a way to force a string constant to be Ansi? check this out

function SomeStrFunc(S: AnsiString): AnsiString; overload;
function SomeStrFunc(S: String): String; overload;

and then when I use SomeStrFunc('ABC') it will call the wide string version. What if I want to use the Ansi version and force Delphi to store 'ABC' constant in AnsiChars.

like image 679
Brian Hawk Avatar asked Oct 07 '11 03:10

Brian Hawk


2 Answers

There is Ansi version of Format function in System.AnsiStrings unit

like image 137
kludg Avatar answered Sep 26 '22 16:09

kludg


Serg answered your question about an AnsiString version of Format(). I'll answer your other question.

String literals are encoded based on the context they are used, so to force a literal to a particular encoding, you have to tell the compiler which encoding it needs to use, eg:

SomeStrFunc(AnsiString('ABC'));

Or

const
  cABC: AnsiString = 'ABC';

SomeStrFunc(cABC);
like image 32
Remy Lebeau Avatar answered Sep 26 '22 16:09

Remy Lebeau