Copy 有 ANSI 版本吗?
Delphi XE下,Copy有ANSI版本吗? 我经常使用 Copy 来复制 ANSI 字符串的片段。
Under Delphi XE, is there an ANSI version for Copy?
I am using Copy a lot to copy pieces of a ANSI strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Delphi 中的 Copy 函数是一个内在函数,这意味着它由编译器而不是运行时库。根据传递给此函数的参数,调用
LStrCopy
或UStrCopy
内部函数检查此示例:
现在检查汇编代码
Altar the Copy function in Delphi is a intrinsic function this means which is handled by the compiler rather than the run-time library. depending of the parameters passed this function call the
LStrCopy
or aUStrCopy
internal functionscheck this sample :
Now check the assembly code
Copy 是一个“编译器魔法”例程,它由编译器根据您传递给它的参数(ANSI 字符串、字符串或动态数组)进行本质上的处理。您可以只使用复制;它可以正确地处理 ANSI 字符串。
Copy is a "compiler magic" routine, it is handled intrinsically by the compiler depending on what parameters you pass it (ANSI string, string, or dynamic array). You can just use Copy; it will work correctly with ANSI strings.
我有同样的问题,请参阅以下代码:
问题是 WideString 必须保存到文件内的 Char 数组中。因此必须进行混合类型...因此,将会出现一些松散的 Unicode 字符,这是无法避免的。
想要的:编译器可以编译它。
解决方案1:在调用 Copy 之前或在 Copy 内部将 WideString 转换为 String。
解决方案2:在分配之前将WideChar 转换为Char。
这里有两个解决方案(记住一些 unicode 字符可能会丢失)...
解决方案 1:
MyFileField[MyIndex]:=Copy(UTF8Encode(TheLiteral),MyIndex,1)[1]; // 注意:Unicode 字符不会丢失,而是会转换,因此请注意口音等...
或
MyFileField[MyIndex]:=Copy(String(TheLiteral),MyIndex,1)[ 1]; // 注意:Unicode 字符会丢失,它们将被转换为 '?'
解决方案 2:
MyFileField[MyIndex]:=Char(Copy(TheLiteral,MyIndex,1)[1]); // 注意:Unicode 字符会丢失,它们将被转换为 '?'
如果有人知道更好的事情,我会很高兴知道。
我个人使用 Copy(String(Literal),Start,NumberOfChars) ,因为普通的重音字母是保守的,更重要的是长度......
示例: Length(String('BlaBlaBlá')) -> 9
示例: Length(UTF8Encode('BlaBlaBlá')) ->自最后一个“á”以来超过 9 个被转换为多个字符等...
希望这可以帮助某人!
I have the same problem, see this code:
The problem is that the WideString must be saved onto am Array of Char inside a file. So mix types must be done... and so, some loose of Unicode chars will occur, no way to avoid it.
The wanted: The compiler can compile it.
Solution1: Convert WideString to String prior to call Copy, or inside Copy.
Solution2: Convert WideChar to Char prior to assing.
Here are both solutions (remember some unicode chars could get lost)...
Solution1:
MyFileField[MyIndex]:=Copy(UTF8Encode(TheLiteral),MyIndex,1)[1]; // Note: Unicode chars will not get lost, but converted, so beware of accent vocals, etc...
or
MyFileField[MyIndex]:=Copy(String(TheLiteral),MyIndex,1)[1]; // Note: Unicode chars will get lost, they will be converted to '?'
Solution2:
MyFileField[MyIndex]:=Char(Copy(TheLiteral,MyIndex,1)[1]); // Note: Unicode chars will get lost, they will be converted to '?'
If anyone knows anthing better i would be glad to know.
I personally use
Copy(String(Literal),Start,NumberOfChars)
since normal accent letters are conserved and more important, length...Example: Length(String('BlaBlaBlá')) -> 9
Example: Length(UTF8Encode('BlaBlaBlá')) -> More than 9 since the last 'á' is converted to multiple chars, etc...
Hope this can help someone!
我做了我自己的职责。它可能很有用,并且可以在 Linux 平台上获得正确的结果,而不是 Copy(string(ansistr), i, l):
I did my own function. It could be useful and get right results instead of Copy(string(ansistr), i, l) on the Linux platform: