Using System.Move() to insert/delete item(s) from an array of string is not as easy as insert/delete it from other array of simple data types. The problem is … string is reference counted in Delphi. Using Move() on reference-counted data types needs deeper knowledge on internal compiler behaviour.
Can someone here explain the needed steps for me to achieve that, or better with some snippet codes, or direct me to a good reference on the internet?
Oh, Please don’t tell me to use the “lazy-but-slow way”, that is, for loop, I know that.
I’ve demonstrated how to delete items from a dynamic array before:
In that article, I start with the following code:
You cannot go wrong with that code. Use whatever value for
Xyou want; in your case, replace it withstring. If you want to get fancier and useMove, then there’s way to do that, too.Since
Xisstring, theFinalizecall is equivalent to assigning the empty string to that array element. I useFinalizein this code, though, because it will work for all array-element types, even types that include records, interfaces, strings, and other arrays.For inserting, you just shift things the opposite direction:
Use
Finalizewhen you’re about to do something that’s outside the bounds of the language, such as using the non-type-safeMoveprocedure to overwrite a variable of a compiler-managed type. UseInitializewhen you’re re-entering the defined part of the language. (The language defines what happens when an array grows or shrinks withSetLength, but it doesn’t define how to copy or delete strings without using a string-assignment statement.)