๐Ÿงต

Delphi String Delete and Insert

Nov 4, 2025

Overview

This lesson covers string manipulation in Delphi using built-in procedures (Part 2). Focus is on the Delete and Insert procedures, which directly modify string variables rather than creating new ones.

String Basics (Recap)

  • A string contains text, numbers, and other characters all treated as text.
  • Example: "Hello World" stored in variable sTemp.
  • Each character has a position (e.g., position 5 is "o", position 6 is a space).
  • Procedures modify the original variable instead of returning a new value.

String Manipulation Procedures

ProcedureParametersDescriptionExample Result
Deletestring, start_position, countRemoves specified number of characters starting from given positionDelete(sTemp, 3, 5) on "Hello World" โ†’ "He World"
Insertsubstring, string, positionInserts substring into string at specified position; shifts remaining charactersInsert("boy", sTemp, 6) on "Hello World" โ†’ "Hello boy World"

Delete Procedure

  • Takes three parameters: target string, starting position (integer), number of characters to delete (integer).
  • Physically modifies the original string variable.
  • Delete(sTemp, 3, 2) starts at position 3 and removes 2 characters (positions 3 and 4).
  • Delete(sTemp, 3, 5) removes positions 3 through 7 (five characters total).
  • If requested count exceeds available characters, deletes only what exists.
  • Example: Delete(sTemp, 9, 1000) on "Hello World" removes last three characters only.
  • Result from Delete(sTemp, 3, 5): "He orld" becomes "He World".

Insert Procedure

  • Takes three parameters: substring to insert, target string, insertion position (integer).
  • Modifies the second parameter (target string) by inserting the first parameter (substring).
  • Shifts all characters from insertion point onwards; does not replace existing content.
  • Insert("boy", sTemp, 6) places "boy" at position 6, shifts remaining text right.
  • If position exceeds string length, substring appends to the end (no padding spaces).
  • Example: Insert("boy", sTemp, 1000) on "Hello World" results in "Hello World boy".
  • Does not create gaps; simply extends the string at the farthest valid position.

Key Terms & Definitions

  • Procedure: Called by name without assignment; modifies parameters directly rather than returning values.
  • Parameter: Value passed to a procedure or function for processing.
  • Substring: A portion of text intended for insertion into another string.
  • Position: Integer representing character location within a string (1-indexed in Delphi).