开发者

delphi 2009 cast to string length 2

looking how to suppress a warning from the compiler that says possible data loss,

st:= copy(str,0,2);

where st is string[2] and str has more then 2 chars.

and copy is defied as from str ret开发者_运维百科urn a new string that is a subset from 0 , 2 places.


This will suppress the warning, but beware the underlying issue is still there: Converting from Unicode to AnsiString can cause lose of data.

st := ShortString(Copy(str,1,2));

And don't forget Delphi stings are 1-based, the first char in the string is 1, not 0.


If you just write:

st := shortstring(str);

The compiler will do the work for you.

It will cut str content to fit the maximum length of st. So if st is defined as st: string[2]; if will retrieve only the 2 first characters of str.

But you may loose non ascii encoded characters in str (not a problem if it does contain only English text).


Your use is wrong you have to do :

st:= copy(str,1,2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜