Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1080131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:58:39+00:00 2026-05-16T21:58:39+00:00

Using System.Move() to insert/delete item(s) from an array of string is not as easy

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:58:40+00:00Added an answer on May 16, 2026 at 9:58 pm

    I’ve demonstrated how to delete items from a dynamic array before:

    • Delphi Q&A: How do I delete an element from an array?

    In that article, I start with the following code:

    type
      TXArray = array of X;
    
    procedure DeleteX(var A: TXArray; const Index: Cardinal);
    var
      ALength: Cardinal;
      i: Cardinal;
    begin
      ALength := Length(A);
      Assert(ALength > 0);
      Assert(Index < ALength);
      for i := Index + 1 to ALength - 1 do
        A[i - 1] := A[i];
      SetLength(A, ALength - 1);
    end;
    

    You cannot go wrong with that code. Use whatever value for X you want; in your case, replace it with string. If you want to get fancier and use Move, then there’s way to do that, too.

    procedure DeleteX(var A: TXArray; const Index: Cardinal);
    var
      ALength: Cardinal;
      TailElements: Cardinal;
    begin
      ALength := Length(A);
      Assert(ALength > 0);
      Assert(Index < ALength);
      Finalize(A[Index]);
      TailElements := ALength - Index;
      if TailElements > 0 then
        Move(A[Index + 1], A[Index], SizeOf(X) * TailElements);
      Initialize(A[ALength - 1]);
      SetLength(A, ALength - 1);
    end;
    

    Since X is string, the Finalize call is equivalent to assigning the empty string to that array element. I use Finalize in 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:

    procedure InsertX(var A: TXArray; const Index: Cardinal; const Value: X);
    var
      ALength: Cardinal;
      TailElements: Cardinal;
    begin
      ALength := Length(A);
      Assert(Index <= ALength);
      SetLength(A, ALength + 1);
      Finalize(A[ALength]);
      TailElements := ALength - Index;
      if TailElements > 0 then begin
        Move(A[Index], A[Index + 1], SizeOf(X) * TailElements);
      Initialize(A[Index]);
      A[Index] := Value;
    end;
    

    Use Finalize when you’re about to do something that’s outside the bounds of the language, such as using the non-type-safe Move procedure to overwrite a variable of a compiler-managed type. Use Initialize when you’re re-entering the defined part of the language. (The language defines what happens when an array grows or shrinks with SetLength, but it doesn’t define how to copy or delete strings without using a string-assignment statement.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; VS says, The type or namespace name 'Formatters' does not
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net;
using System; namespace FirstApplication { class Program { static void Main(params string[] args) {
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Data; using System.Data.SqlClient; I am using a SqlCommand cmd.Parameters of SqlDbType.DateTime to create
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
I am working with two timers: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;
I'm currently using the following snippet to insert data into a table in my
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
This is an example of using XPathNavigator from Microsoft . using System; using System.Xml;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.