2

I know that the following deletes four characters starting with position 3 in the string in question ('12345678') in Delphi:

    var
      Source : string;

    begin
      Source := '12345678';
      delete(Source, 3, 4);    // Delete the 3rd, 4th, 5th and 6th characters
      ShowMessage('Source now : '+Source);
    end;

But how do I delete multiple substrings? For example, if I wanted to delete 2 & 3 as well as 5 & 6 in the string '12345678' so as to get '1478.' Thanks.

9
  • Delete two characters starting at 5 and then delete two characters starting at 2; Two lines of code. Commented Mar 9, 2018 at 23:34
  • So it does require two lines. Thanks. Commented Mar 9, 2018 at 23:42
  • Source:=copy(Source,1,1) + copy(Source,4,1) + copy(Source,7,MaxInt); Commented Mar 9, 2018 at 23:54
  • 2
    Be aware that the solution suggested by @JanLauridsen results in three additional temporary string allocations (in addition to the original Source one) that don't exist in the two-line solution. Saving a line of code isn't always the best solution. Commented Mar 10, 2018 at 0:09
  • 2
    Sometimes it's the clarity that matters. The problem with using Delete here is that after the first call, the indices have shifted. Which is why Ken suggested working from right to left. One might argue that the Copy version was easier to read. Commented Mar 10, 2018 at 0:29

1 Answer 1

2

The simplest (and probably most efficient) way is simply to do it with two calls to Delete, first to delete two characters starting at 5 and then to delete two characters starting at 2. (If you start with 2 first, it shifts the indices for the remaining characters, as David pointed out in a comment to the question).

var
  Source : string;
begin
  Source := '12345678';
  Delete(Source, 5, 2);  
  Delete(Source, 2, 2); 
  ShowMessage('Source now : '+Source);
end;

@JanLaundsen suggested in a comment to the question that you can also use Copy to do it in one line:

Source := Copy(Source, 1, 1) + Copy(Source, 4, 1) + Copy(Source, 7, MaxInt);

This solution works, but adds the overhead of three function calls (the calls to Copy) plus three temporary string allocations (one for the result of each call to Copy) that do not happen in the two line calls to Delete. (Saving a line of code isn't always the best solution). IMO, the two separate calls are also much easier to read, but that's a personal opinion. It may not be more readable to you.

Sign up to request clarification or add additional context in comments.

10 Comments

It's possible to do this with a single memory reallocation, so two Deletes won't be the most efficient. Not that I think it matters very much, just the mathematician in me coming to the fore.
@DavidHeffernan: OK. I said probably, not absolutely. I made it community-wiki; if it bothers you, fee free to edit to take it out. It's definitely more efficient than the Copy technique.
I seem to have set off an interesting discussion among the experts. Honestly, I am happy to get all your inputs, as they work. I can see that two lines of delete (with yes, the downstream fragment before the upstream one; I caught that clever code yesterday - thanks @Ken). I am happy to see my programs work at this point. I hope to master coding enough one day to worry about efficiency. Thanks again.
I finished my program and everything is working fine, including the deletion of substring(s) using the solution of @Ken above, except for one small weird detail. The deletion is one character off. For example, if it is supposed to delete characters 100 to 110, it is deleting characters 99 to 109. So I added +1 to the code (e.g., 5+1 in the first delete line of his code above). But then it deletes characters 101 to 111. Any idea why?
@Serge: That's impossible. What Delphi version are you using? What OS are you building for?
|

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.