I've tried the following code without success.
string.remove[index];
string.insert[index];
Can anyone suggest how can I correct this code?
Just do this:
stringVariable[index] := 'A';
Test := 'Testing'; Test[3] := 'x'; WriteLn(Test); ReadLn;, and don't get any compiler warning in a Win32 console app.You can directly access a string like it was a character array in Delphi:
MyString[Index] := NewChar;
For multiple character deletions and inserts, the System unit provides Delete and Insert:
System.Delete(MyString, Index, NumChars);
System.Insert(NewChars, MyString, Index);
Recent versions of Delphi provide the helper functions to string:
MyString := MyString.Remove(3, 1);
MyString := MyString.Insert(3, NewChars);
Once again, please read the tutorials I've referred you to twice previously. This is a very basic Pascal question that any of them would have answered for you.
Here is another way to do it. Have fun with Delphi
FUNCTION ReplaceCharInPos(CONST Text: String; Ch: Char; P: Integer): String;
VAR Lng: Integer; left, Right: String;
BEGIN
Lng:= Length(Text);
IF Lng = 0 THEN
Result:= ''
ELSE IF (P < 1) OR (P > Lng) THEN
Result:= Text
ELSE IF P = 1 THEN
Result:= Ch + Copy(Text, 2, Lng - 1)
ELSE IF P = Lng THEN
Result:= Copy(Text, 1, Lng - 1) + Ch
ELSE BEGIN
Left:= Copy(Text, 1, P - 1);
Right:= Copy(Text, P + 1, Lng - P);
Result:= Left + Ch + Right;
END;
END;