1
procedure TTelephoneNumberConverter.btnConvertClick(Sender: TObject);
var
  number: string;
  dupe: string;
  converted: string;
begin
  number := edtInput.Text ;
  dupe := Copy(number, 4, 1) ;
  converted := Insert(dupe , number , 4 ) ;
  pnlOutput.Caption := converted;
end;

Ok guys I just have a quick question regarding Delphi 2010 and inserting strings into other strings. The purpose of this small piece of code is to take the 4th character in a specific string and to duplicate it and add it next to the specific character e.g. 12345 -> 123445

The only problem is I keep getting an error :

Incompatible types 'string' and 'procedure, untyped pointer or untyped parameter'.

I am probably missing something small and stupid but would appreciate if someone could maybe answer my question.

1
  • 1
    I am probably missing something: You mean like the System.Insert documentation or the pop-up window that Code Insight gives you that tells you it's a procedure and not a function? Commented Feb 11, 2016 at 4:18

1 Answer 1

2

Insert is a procedure that modifies its second argument.

Its signature is:

procedure Insert(Source: string; var Dest: string; Index: Integer);

The compiler error you see occurs because Insert does not return anything and thus cannot be the rhs of an assignment.

Your code should therefore be:

converted := number;
Insert(dupe, converted, 4);

Copy is overkill for a single character. Use [] instead:

dupe := number[4];
Sign up to request clarification or add additional context in comments.

1 Comment

Something to note is that if you are to use direct character indexing then you also need to add a length check to deal with a number less than 4 characters in length, to avoid the exception that number[4] will then result in. Copy avoids this since it will simply return an empty string if the input string is shorter than the indicated start position resulting in a no-op when number is < 4 characters in length. Adding those safeguards for an insignificant (in this case) efficiency gain is arguably greater overkill than simply leveraging the safeguards already built in to Copy().

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.