Strings

InfoOut.asString(Format('Active voices: %d', [VoiceList.Count]));

StringList

TStringList remains the most convenient container for working with comma-text, file lists, and string caches.

Procedure Description
function TStringList.Create: TStringList; Create a new instance of a string list
function TStringList.GetCommaText: String; Return all strings as a single comma-separated string
procedure TStringList.SetCommaText(Comma: string); Set the list contents from a comma-separated string
function TStringList.GetText: String; Return all strings concatenated with line breaks
procedure TStringList.SetText(Text: string); Replace the list contents with text split by line breaks
procedure TStringList.Sort; Sort the list in ascending order
procedure TStringList.SortDesc; Sort the list in descending order
procedure TStringList.SaveToFile(FileName: string); Save the list contents to a file. If no path is given, defaults to the last used location in Usine
procedure TStringList.AppendToFile(FileName: string); Append the list contents to the end of an existing file
procedure TStringList.LoadFromFile(FileName: string); Load list contents from a file. If no path is given, defaults to the last used location in Usine
procedure TStringList.Clear; Clear all strings from the list
procedure TStringList.Free; Free the memory allocated for the list
function TStringList.GetStrings(Index: integer): String; Get the string at the specified index
procedure TStringList.SetStrings(Index: integer; Value: String); Set the string value at the specified index
procedure TStringList.Add(Value: String); Add a new string to the end of the list
procedure TStringList.Delete(Index: integer); Delete the string at the specified index
procedure TStringList.Delete(Value: integer); Delete the string matching the specified value if it exists
function TStringList.IndexOf(Value: string): integer; Return the index of the specified string, or -1 if not found
function TStringList.Count: integer; Return the number of strings in the list
function TStringList.GetCount: integer; Same as Count; return the total number of strings in the list
////////////////////////////////////////////////////////////////////////////////
// TStringList Methods Demo - showcases core TStringList helpers available in
// scripts. Press "run demo" to populate the lists and review the outputs.
////////////////////////////////////////////////////////////////////////////////
const DEMO_FILE_NAME = 'tstringlist-demo.txt'; // file created alongside the patch

var
  RunDemoBtn   : TParameter;
  ResetBtn     : TParameter;
  ListTextOut  : TParameter;
  CommaTextOut : TParameter;
  InfoOut      : TParameter;
  DemoList     : TStringList;
  HelperList   : TStringList;
  KeyValueList : TStringList;

procedure ResetDemoState;
begin
  DemoList.Clear;
  HelperList.Clear;
  KeyValueList.Clear;
  ListTextOut.asString('');                  // reset visible buffers
  CommaTextOut.asString('');
  InfoOut.asString('Press "run demo" to execute the TStringList walkthrough.');
end;

procedure Init;
begin
  ModuleColor($FF00897B);
  RunDemoBtn   := CreateParam('run demo',  ptButton,    pioInput);
  ResetBtn     := CreateParam('reset',     ptButton,    pioInput);
  ListTextOut  := CreateParam('list lines',ptTextField, pioOutput);
  CommaTextOut := CreateParam('comma text',ptTextField, pioOutput);
  InfoOut      := CreateParam('info',      ptTextField, pioOutput);

  DemoList.create;
  HelperList.create;
  KeyValueList.create;

  ResetDemoState;
end;

procedure Destroy;
begin
  DemoList.free;
  HelperList.free;
  KeyValueList.free;
end;

procedure RunStringListDemo;
var
  i        : integer;
  logText  : string;
  infoText : string;
  fileInfo : string;
begin
  // Clear existing content before rebuilding the demonstration payload.
  DemoList.Clear;                            // Clear
  HelperList.Clear;
  KeyValueList.Clear;

  // Populate the main list one entry at a time.
  DemoList.Add('kick');                      // Add
  DemoList.Add('snare');
  DemoList.Add('hihat');

  // Parse a comma-separated string into HelperList, then merge it into DemoList.
  HelperList.SetCommaText('"ride","crash","china"'); // SetCommaText / Count
  for i := 0 to HelperList.Count - 1 do
    DemoList.Add(HelperList.GetStrings(i));  // Add + GetStrings

  // Update an existing entry in-place.
  DemoList.SetStrings(2, 'hihat (closed)');  // SetStrings

  // Look up and remove a specific token.
  i := DemoList.IndexOf('china');            // IndexOf
  if i <> -1 then
    DemoList.Delete(i);                      // Delete

  // Demonstrate alphabetical sort helpers.
  DemoList.Sort;                             // Sort (ascending)
  DemoList.SortDesc;                         // SortDesc (descending)

  // Append a multi-line payload using the line-based helpers.
  HelperList.SetText('cowbell' + #13#10 + 'tambourine'); // SetText
  DemoList.SetText(DemoList.GetText + HelperList.GetText); // GetText + SetText

  // Persist and re-use name/value pairs with a separate list.
  KeyValueList.SetCommaText('"tempo=120","swing=0.30","bars=4"');
  KeyValueList.SetStrings(1, 'swing=0.35');  // tweak value
  infoText := 'Pairs: ' + KeyValueList.GetCommaText;

  // Write current content to disk, append extra pairs, then reload.
  DemoList.SaveToFile(DEMO_FILE_NAME);       // SaveToFile
  KeyValueList.AppendToFile(DEMO_FILE_NAME); // AppendToFile
  HelperList.LoadFromFile(DEMO_FILE_NAME);   // LoadFromFile
  fileInfo := 'Saved to ' + DEMO_FILE_NAME +
              ', reloaded lines=' + IntToStr(HelperList.Count);

  // Surface the generated content inside the module UI.
  logText := DemoList.GetText;               // GetText
  ListTextOut.asString(logText);
  CommaTextOut.asString(DemoList.GetCommaText); // GetCommaText
  InfoOut.asString('Items=' + IntToStr(DemoList.Count) +
                   '; ' + infoText +
                   '; ' + fileInfo);
end;

procedure Callback(N: integer);
begin
  if N = RunDemoBtn then
    RunStringListDemo;

  if N = ResetBtn then
    ResetDemoState;
end;

TStringList.Create

function TStringList.Create: TStringList;

Initializes the list instance. Always call Create before use.

items.Create;

TStringList.SetCommaText

procedure TStringList.SetCommaText(Comma: string);

Populates the list from a comma-text literal.

items.SetCommaText('kick,snare,hat');

TStringList.GetCommaText

function TStringList.GetCommaText: String;

Returns the comma-text representation of the list.

pattern.asString(items.GetCommaText);

TStringList.GetText

function TStringList.GetText: String;

Loads a multi-line Pascal string into the list.

 s := items.GetText;

TStringList.SetText

procedure TStringList.SetText(Text: string);

Loads a multi-line Pascal string into the list.

items.SetText('row1'#13#10'row2');

TStringList.Add

procedure TStringList.Add(Value: String);

Appends one entry.

items.Add(Format('Voice %d', [voiceIndex]));

TStringList.SetStrings

procedure TStringList.SetStrings(Index: integer; Value: String);

Replaces an item at the given index.

items.SetStrings(i, 'Gate');

TStringList.GetStrings

function TStringList.GetStrings(Index: integer): String;

Reads an item.

Trace(items.GetStrings(i));

TStringList.Delete (index)

procedure TStringList.Delete(Index: integer);

Removes the element at the supplied index.

items.Delete(0);

TStringList.Delete (value)

procedure TStringList.Delete(Value: integer);

Deletes a value if present.

items.Delete('Solo');

TStringList.IndexOf

function TStringList.IndexOf(Value: string): integer;

Returns the index of a value or -1 when not found.

idx := items.IndexOf('Solo');

TStringList.Sort

procedure TStringList.Sort;

Sorts ascending, matching the Browser panel.

items.Sort;

TStringList.SortDesc

procedure TStringList.SortDesc;

Sorts descending.

items.SortDesc;

TStringList.SaveToFile

procedure TStringList.SaveToFile(FileName: string);

Writes the list to disk. Paths resolve relative to the Usine workspace if no folder is specified.

items.SaveToFile('Scripts/notes.txt');

TStringList.AppendToFile

procedure TStringList.AppendToFile(FileName: string);

Appends to an existing file without clearing prior contents.

items.AppendToFile('Logs/automation.txt');

TStringList.LoadFromFile

procedure TStringList.LoadFromFile(FileName: string);

Reads a file back into the list.

items.LoadFromFile('Scripts/notes.txt');

TStringList.Clear

procedure TStringList.Clear;

Deletes all entries.

items.Clear;

TStringList.Count

function TStringList.Count: integer;

Returns the number of entries.

for i := 0 to items.Count - 1 do
  Trace(items.GetStrings(i));

TStringList.Free

procedure TStringList.Free;

Releases the list in Destroy.

Procedure Destroy
begin
   items.Free;
end;

String helpers

Procedure Description
function string.StartsWith(AValue: string): boolean; Return True if the string begins with the specified substring AValue
function string.EndsWith(AValue: string): boolean; Return True if the string ends with the specified substring AValue

Strings

Procedure Description
type TTextLineBreakStyle = (tlbsLF, tlbsCRLF, tlbsCR); Defines possible line break styles (Line Feed, Carriage Return + Line Feed, Carriage Return)
type TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase); Flags used for string replacement options
function UpperCase(s: string): string; Convert all characters in a string to uppercase
function LowerCase(s: string): string; Convert all characters in a string to lowercase
function UpCase(c: AnsiChar): AnsiChar; Convert a single ANSI character to uppercase
function UpCase(c: WideChar): WideChar; Convert a single wide character to uppercase
function CompareStr(s1, s2: string): Int32; Compare two strings case-sensitively
function CompareMem(p1, p2: Pointer; Length: PtrUInt): EvalBool; Compare two memory blocks for equality
function CompareText(s1, s2: string): Int32; Compare two strings ignoring case
function SameText(s1, s2: string): EvalBool; Return True if two strings are equal ignoring case
function Copy(s: string; ifrom, icount: Longint): string; Copy a substring from a string
procedure Delete(var s: string; ifrom, icount: Longint); Delete a substring from a string
procedure Insert(s: string; var s2: string; ipos: Longint): string; Insert a substring into another string at a given position
function StrGet(var S: String; i: Integer): Char; Get the character at index i in a string
procedure StrSet(c: Char; I: Integer; var s: String); Set a specific character in a string at position I
function Length(s: String): Longint; Return the number of characters in a string
procedure SetLength(var S: String; L: Longint); Set the length of a string
function AnsiUpperCase(s: string): string; Convert a string to uppercase using locale-specific rules
function AnsiLowerCase(s: string): string; Convert a string to lowercase using locale-specific rules
function AnsiCompareStr(s1, s2: string): Int32; Compare two strings case-sensitively using ANSI rules
function AnsiCompareText(s1, s2: string): Int32; Compare two strings case-insensitively using ANSI rules
function AnsiSameText(s1, s2: String): EvalBool; Return True if two strings are equal ignoring case (ANSI version)
function AnsiSameStr(s1, s2: String): EvalBool; Return True if two strings are exactly equal (ANSI version)
function Trim(s: string): string; Remove leading and trailing spaces from a string
function TrimLeft(s: string): string; Remove leading spaces from a string
function TrimRight(s: string): string; Remove trailing spaces from a string
function PadL(s: string; Len: SizeInt; c: Char = ' '): string; Pad a string on the left to a given length using the specified character
function PadR(s: string; Len: SizeInt; c: Char = ' '): string; Pad a string on the right to a given length using the specified character
function Padz(s: string; I: LongInt): string; Pad a string on the left with zeros instead of spaces
function QuotedStr(s: string): string; Return a quoted version of a string
function AnsiQuotedStr(s: string; Quote: Char): string; Return a string enclosed by a specific quote character
function AnsiDequotedStr(s: string; AQuote: Char): string; Remove quotes from a quoted string
function WrapText(Line, BreakStr: string; BreakChars: set of AnsiChar; MaxCol: Int32): string; Wrap a text line into multiple lines based on maximum column width
function AdjustLineBreaks(s: string; Style: TTextLineBreakStyle): string; Convert line breaks to a given line break style
function IntToHex(Value: Int64; Digits: Int32 = 1): string; Convert an integer value to a hexadecimal string
function IntToHex(Value: UInt64; Digits: Int32 = 1): string; Convert an unsigned integer value to a hexadecimal string
function IntToStr(i: Int64): string; Convert an integer to its string representation
function IntToStr(i: UInt64): string; Convert an unsigned integer to its string representation
function StrToInt(s: string): Int32; Convert a string to an integer
function StrToIntDef(s: string; Def: Int32): Int32; Convert a string to an integer, returning a default value on error
function StrToInt64(s: string): Int64; Convert a string to a 64-bit integer
function StrToInt64Def(s: string; Def: Int64): Int64; Convert a string to a 64-bit integer, returning a default on error
function StrToUInt64(s: string): UInt64; Convert a string to an unsigned 64-bit integer
function StrToUInt64Def(s: string; Def: UInt64): UInt64; Convert a string to an unsigned 64-bit integer, returning a default on error
function FloatToStr(f: Extended): string; Convert a floating-point value to a string
function StrToFloat(s: string): Extended; Convert a string to a floating-point value
function StrToFloatDef(s: string; Def: Extended): Extended; Convert a string to a float, returning a default value on error
function CurrToStr(Value: Currency): string; Convert a currency value to a string
function StrToCurr(s: string): Currency; Convert a string to a currency value
function StrToCurrDef(s: string; Def: Currency): Currency; Convert a string to a currency value, returning a default on error
function StrToBool(s: string): EvalBool; Convert a string to a boolean value
function BoolToStr(B: EvalBool; TrueS: string = 'True'; FalseS: string = 'False'): string; Convert a boolean value to its string representation
function StrToBoolDef(s: string; Default: EvalBool): EvalBool; Convert a string to a boolean value, returning a default on error
function Format(Fmt: string; Args: array of Variant): string; Format a string using placeholders and argument values. see format-string
function FormatFloat(Format: string; Value: Extended): string; Format a floating-point value according to a format string
function FormatCurr(Format: string; Value: Currency): string; Format a currency value according to a format string
function LastDelimiter(Delimiters, s: string): SizeInt; Return the position of the last delimiter character in a string
function StringReplace(S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string; Replace occurrences of a substring within a string using flags for case and repetition
function IsDelimiter(Delimiters, s: string; Index: SizeInt): EvalBool; Check if a character at a given position is one of the specified delimiters
function Pos(Substr: string; Source: string): SizeInt; Return the position of a substring within another string
function StringOfChar(c: Char; l: SizeInt): string; Create a string made of the same character repeated l times
function Replicate(c: Char; I: LongInt): string; Build a string containing I repetitions of a character

more about scripts

version 7.0.250121

Edit All Pages