2

I've tried the following code without success.

string.remove[index];
string.insert[index];

Can anyone suggest how can I correct this code?

3 Answers 3

8

Just do this:

stringVariable[index] := 'A';
Sign up to request clarification or add additional context in comments.

4 Comments

It's valid for most of delphi, but that kind of code now is depricated. If you need to change string, you must use TStringBuilder class for that kind of work.
@DavidHeffernan: it is not deprecated just yet, but it may be in the not-too-distant future. The current version already issues warnings about it now.
@Remy: I just tested in XE4, with Test := 'Testing'; Test[3] := 'x'; WriteLn(Test); ReadLn;, and don't get any compiler warning in a Win32 console app.
It is not deprecated in the Windows/OSX compilers yet, but it is deprecated in the iOS mobile compiler and documented as such: "the Delphi mobile compilers emit the warning W1068 Modifying strings in place may not be supported in the future (Delphi). At some point, this warning is to be replaced by an error"
7

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.

4 Comments

I TRIED and compiler gives me fail!
@Diego no it does not "give you fail!" - it gives you a specific error message, which you should READ and post in your original question if you don't understand!
Would the downvoter please explain what's wrong with my answer, both so I can correct it and so I can see the problem? Thanks.
@David: Not that important, and usually I just ignore them. I actually thought I'd covered the bases pretty well here, though, so I was just curious what I'd missed. I still don't see anything wrong, so it's most likely either someone being extremely petty or someone getting revenge for something.
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.