Trying to move project from Delphi 2007 to Delphi XE4. What is the best way to convert String to AnsiString in Delphi XE4?
试图将项目从Delphi 2007转移到Delphi XE4。在Delphi XE4中,将字符串转换为AnsiString的最佳方式是什么?
1 个解决方案
#1
8
You simply assign it:
你只是分配:
var
AnsiStr: AnsiString;
Str: string;
....
AnsiStr := Str;
The compiler will emit a warning mind you:
编译器会发出警告提醒你:
W1058 Implicit string cast with potential data loss from 'string' to 'AnsiString'
You can use a cast to suppress that warning:
您可以使用cast来抑制该警告:
AnsiStr := AnsiString(Str);
By default that gives no warning, although there is of course still potential for data loss. If you enable warning W1060 then the compiler says:
默认情况下,它不会发出任何警告,尽管数据丢失的可能性仍然存在。如果启用警告W1060,编译器会说:
W1060 Explicit string cast with potential data loss from 'string' to 'AnsiString'
Of course, it's not expected that Delphi XE4 code has much place for the use of AnsiString
. Unless you have a very specific interop requirement, then text is best held in the native data type, string
. If you want to operate on byte arrays use TBytes
or TArray<Byte>
.
当然,并不期望Delphi XE4代码有很多地方可以使用AnsiString。除非您有非常特定的互操作需求,否则文本最好保存在本机数据类型string中。如果要对字节数组进行操作,请使用TBytes或TArray< byte >。
#1
8
You simply assign it:
你只是分配:
var
AnsiStr: AnsiString;
Str: string;
....
AnsiStr := Str;
The compiler will emit a warning mind you:
编译器会发出警告提醒你:
W1058 Implicit string cast with potential data loss from 'string' to 'AnsiString'
You can use a cast to suppress that warning:
您可以使用cast来抑制该警告:
AnsiStr := AnsiString(Str);
By default that gives no warning, although there is of course still potential for data loss. If you enable warning W1060 then the compiler says:
默认情况下,它不会发出任何警告,尽管数据丢失的可能性仍然存在。如果启用警告W1060,编译器会说:
W1060 Explicit string cast with potential data loss from 'string' to 'AnsiString'
Of course, it's not expected that Delphi XE4 code has much place for the use of AnsiString
. Unless you have a very specific interop requirement, then text is best held in the native data type, string
. If you want to operate on byte arrays use TBytes
or TArray<Byte>
.
当然,并不期望Delphi XE4代码有很多地方可以使用AnsiString。除非您有非常特定的互操作需求,否则文本最好保存在本机数据类型string中。如果要对字节数组进行操作,请使用TBytes或TArray< byte >。