none
none

Reputation: 4827

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 return a new string that is a subset from 0 , 2 places.

Upvotes: 2

Views: 403

Answers (3)

philnext
philnext

Reputation: 3402

Your use is wrong you have to do :

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

Upvotes: 0

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43033

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).

Upvotes: 4

Cosmin Prund
Cosmin Prund

Reputation: 25678

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.

Upvotes: 5

Related Questions