Delphi XE3 - cannot concatenate strings

In my life I cannot combine two (/ three) lines. These are some codes I tried:

dir := 'C:\Users\' + Username + '\Downloads\done.txt'; //"Username" is the computer current username.
//another example vvv
dir := 'C:\Users\' + Username;
dir := dir + '\Downloads\done.txt';
//last example vvv
dir := Concat('C:\Users\', Username, '\Downloads\done.txt');

All examples always return the same result:

C: \ Users \ -username -

Never

C: \ Users \ -username- \ Downloads \ Done.txt

What am I doing wrong here?

+5
source share
1 answer

I assume that your variable Usernamecontains # 0 at the end and you output this variable to a specific Windows API function. For example, the following code will result in this incorrect behavior:

procedure TForm1.Button1Click(Sender: TObject);
var
  Dir: string;
  Username: string;
begin
  Username := 'Username' + #0;
  Dir := Concat('C:\Users\', Username, '\Downloads\done.txt');
  ShowMessage(Dir);
end;

My suggestion is to check the value of your variable Usernameand remove the extra # 0 at the end, if any.

+16
source

All Articles