Copying string content to char array

Bharat picture Bharat · Dec 14, 2010 · Viewed 27.4k times · Source

I want to copy the content in the string to char array.

Can I use this code StrLCopy(C, pChar(@S[1]), high(C));

I am currently using Delphi 2006. Will there be any problems if I upgrade my Delphi version because of Unicode support provided in newer versions?

If not, what can be the code for this conversion?

Answer

Rob Kennedy picture Rob Kennedy · Dec 14, 2010

When you're copying a string into an array, prefer StrPLCopy.

StrPLCopy(C, S, High(C));

That will work in all versions of Delphi, even when Unicode is in effect. The character types of C and S should be the same; don't try to use that function to convert between Ansi and Unicode characters.

But StrLCopy is fine, too. You don't need to have so much pointer code, though. Delphi already knows how to convert a string into a PChar:

StrLCopy(C, PChar(S), High(C));