LeftPart function
String / Filename routines
Return to Introduction  Previous page  Next page
Unit
acUtils  

Declaration
function LeftPart(const SubStr, Str: String): String;  

Description
The LeftPart function finds the first occurence of any sub-string (SubStr parameter) in specified string (Str parameter) and returns the part of string (SubStr) BEFORE the separator (Str). If the SubStr can not be found in the Str, the function returns empty string.  
 
tip If you would like to find LAST occurence of sub-string in specified string — use LeftPartR function.  

Example
// Get first line from any text which contains line-breaks  
FirstLine := LeftPart(#13#10, Str);  
if FirstLine = '' then  
  FirstLine := LeftPart(#10, Str);  

Original code
function LeftPart(const SubStr, Str: String): String;  
var  
  I: Integer;  
begin  
  I := Pos(SubStr, Str);  
  if I <> 0 then  
    Result := Copy(Str, 1, I - 1)  
  else  
    Result := '';  
end;  

See also
LeftPart, RightPart, LeftPartR, RightPartR, PosR and SplitStr routines.