TSubstringList.Put Method

procedure Put(const Index: Integer; const S: string); overload;

Writes S into item Index; S takes every text kind.

#NameDescription
1Index

The item's index, 0..Count - 1.

2S

The text; it may be an item of this list, this one included.

Result: stored in self (calling object)

Remarks:

List[Index] := S takes a Substring only, stored as an assignment stores it: a view stays a view, so List[j] := List[i] reads item i's characters until item i is written or deleted, and dangles after that, as designed, as a local's view does. Put takes every text kind. A sorted list raises EStringListError.

Examples

Rewriting an item.

uses Substr;

procedure Example;
var List: TSubstringList;
begin
    List := TSubstringList.Create(4);
    try
        List.Add('old');
        List.Put(0, 'new');
        Assert(List[0] = 'new');
        List[0] := List[0].Substring(1);
        Assert(List[0] = 'ew');
    finally
        List.Free;
    end;
end;

A view of another item, then a copy of its own.

uses Substr;

procedure Example;
var List: TSubstringList;
    s: Substring;
begin
    List := TSubstringList.Create(4);
    try
        List.Add('first');
        List.Add('second');
        List[1] := List[0];
        Assert(List[1].Data = List[0].Data);
        List.Items[0].UpperCaseAscii;
        Assert(List[1] = 'FIRST');
        s := List[1];
        s.MakeUnique;
        List[1] := s;
        List.Items[0].LowerCaseAscii;
        Assert((List[0] = 'first') and (List[1] = 'FIRST'));
    finally
        List.Free;
    end;
end;
See Also: TSubstringList.Strings, TSubstringList.Items