介绍
本文章介绍了Delphi XE5、6、7在Android或者IOS上使用AnsiString和AnsiChar,在移动平台上面,一般都是使用String类型来处理数据,但是很多老的代码就无法在移动上使用,因为老的String是AnsiString,为了解决这个问题,所以开发了附件中的代码,用来解决这个问题。
例如,我们使用ORawByteString来解决问题
procedure TestAnsiString(const aOutputLines: TStrings); var Raw: RawByteString; Wide: string; I: Integer; C: PAnsiChar; begin Raw := RawByteString('HEADET'); Raw[AnsiLength(Raw)] := AnsiChar('R');//Raw = HEADER Wide := UTF8ToString(Raw);//convert "HEADER" from UTF-8 (ASCII) to UTF-16 aOutputLines.Clear; aOutputLines.Add(Wide);//output "HEADER" Raw := RawByteString(Wide);//CONVERT UTF-16 back into ASCII (without any codepage) I := Pos('H', Raw);//Fast search in RawByteString Raw := AnsiCopy(Raw, I, 3);//Use fast copy on RawByteString -> Raw = "HEA" Wide := String(Raw);//CONVERT ASCII to UTF-16 (without any codepage) C := PAnsiChar(Raw);//YOU CAN EVEN USE PAnsiChar!!! Raw := RawByteString(C);//AND convert PAnsiChar back to RawByteString/AnsiString C := PAnsiChar(Raw); aOutputLines.Add(String(C[2]));//output "A" (the third letter from C (PAnsiChar is always 0-based) AnsiDelete(Raw, 1, 1);//Fast delete in RawByteString -> Raw = "EA" AnsiInsert(RawByteString('x'), Raw, 2);//Fast insert -> Raw = "ExA" Raw := UpperCase(Raw);//Fast uppercase -> Raw = "EXA" aOutputLines.Add(String(Raw));//output "EXA" Raw := LowerCase(Raw);//Fast uppercase -> Raw = "exa" aOutputLines.Add(String(Raw));//output "exa" end;
通过上面的代码,我们可以发现,我们甚至可以使用PAnsiChar,在单元里可以看到一些我们以前delphi 7使用的一些对象
AnsiString = TORawByteString;
AnsiChar = TORawByteChar;
RawByteString = TORawByteString;
PAnsiChar = PORawByteChar;
PRawByteChar = PORawByteChar;
通过这个单元就很容易将原来的代码进行移植,非常好的东西,有兴趣的可以下载下来试试。
关键字词: