RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrLCat Function

Appends up to a specified maximum number of characters to string.

Pascal
function StrLCat(Dest: PAnsiChar; const Source: PAnsiChar; MaxLen: Cardinal): PAnsiChar; overload;
function StrLCat(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar; overload;
C++
PAnsiChar StrLCat(PAnsiChar Dest, const PAnsiChar Source, unsigned MaxLen);
PWideChar StrLCat(PWideChar Dest, const PWideChar Source, unsigned MaxLen);

StrLCat appends at most MaxLen - StrLen(Dest) characters from Source to the end of Dest and returns Dest. That is, MaxLen indicates the maximum length that is allowed in the result string.  

C++ Examples: 

 

/*
The following example uses an edit control and a button on a
form in an application. When the button is clicked, the edit
control’s text is split in halves that are copied into
separate buffers. Then the contents of the buffers are
displayed in a message box.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  AnsiString EditText = AnsiString(Edit1->Text);
  char* szFirstHalf = new char[StrLen(EditText.c_str())/2+2];
  char* szSecondHalf = new char[StrLen(EditText.c_str())/2+2];
  char* pszSecondHalf = &EditText[StrLen(EditText.c_str())/2+1];

  *szFirstHalf = 0;
  *szSecondHalf = 0;

  StrLCat(szFirstHalf, EditText.c_str(), StrLen(EditText.c_str())/2);
  StrCat(szSecondHalf, pszSecondHalf);

  Application->MessageBox(WideString(szFirstHalf).c_bstr(), L"First Half", MB_OKCANCEL);
  Application->MessageBox(WideString(szSecondHalf).c_bstr(), L"Second Half", MB_OKCANCEL);

  delete [] szFirstHalf;
  delete [] szSecondHalf;
}

 

Delphi Examples: 

{
The following example uses an edit control and a button on a
form in an application. When the button is clicked, the edit
control’s text is split in halves that are copied into
separate buffers. Then the contents of the buffers are
displayed in a message box.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  FirstHalf: PChar;
  SecondHalf: PChar;
  HalfLen: Integer;
begin
  HalfLen := StrLen(PChar(string(Edit1.Text))) div 2;
  GetMem(FirstHalf,HalfLen+2);
  GetMem(SecondHalf,HalfLen+2);
  FirstHalf^ := Chr(0);
  SecondHalf^ := Chr(0);
  StrLCat(FirstHalf, PChar(string(Edit1.Text)), HalfLen);
  StrCat(SecondHalf, PChar(string(Edit1.Text)) + HalfLen);
//  Application.MessageBox(FirstHalf, 'First Half', [smbOK], smsInformation, smbOK, smbCancel);
  Application.MessageBox(FirstHalf, 'First Half', MB_OKCANCEL);
  Application.MessageBox(SecondHalf, 'Second Half', MB_OKCANCEL);
  FreeMem(FirstHalf);
  FreeMem(SecondHalf);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!