Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Array of Char to String? [closed]

Tags:

delphi

How do I convert an array of char to a string?

I have a normal array of characters and I want to convert its values to string. How can I do this?

Edit: Originally this question asked about "array of string to string", but the OP accepted an answer that said "array of char, to string".

like image 593
john12 Avatar asked Sep 02 '11 10:09

john12


2 Answers

It seems that perhaps you have text in an array of char. If so then you can do this:

function ArrayToString(const a: array of Char): string;
begin
  if Length(a)>0 then
    SetString(Result, PChar(@a[0]), Length(a))
  else
    Result := '';
end;

On the other hand, maybe you're asking a completely different question.

like image 122
David Heffernan Avatar answered Oct 15 '22 06:10

David Heffernan


function ArrayToString(const Data: array of string): string;
var
  SL: TStringList;
  S: string;
begin
  SL := TStringList.Create;
  try
    for S in Data do
      SL.Add(S);
    Result := SL.Text;
  finally
    SL.Free;
  end;
end;

This is how I understand what you are asking. It could be that David's solution is what you want, however. You decide.

like image 32
Rudy Velthuis Avatar answered Oct 15 '22 06:10

Rudy Velthuis