Returns number of characters in a string excluding the null terminator.
StrLen returns the number of characters in Str, not counting the null terminator.
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!
|