SplitStr procedure
String / Filename routines
Return to Introduction  Previous page  Next page
Unit
acUtils  

Declaration
type  
  TacSplitStrSide = (LEFT, RIGHT);  
  TacSplitStrSides = set of TacSplitStrSide;  
 
procedure SplitStr(const SubStr, Str: String;  
            var LeftPart, RightPart: String;  
            MainSide: TacSplitStrSide = LEFT;  
            LeaveSeparatorOn: TacSplitStrSides = []);  

Description
The SplitStr procedure separates the string specified in Str parameter on two parts (FirstPart and SecondPart parameters) . The separator character or string specified in SubStr parameter.  
 
MainSide specifies the part which should contains an entire string (Str), if SubStr not found in Str. This value can be LEFT or RIGHT.  
 
LeaveSeparatorOn specifies the separated parts where you would like to leave the separator string.  

Example
SplitStr('@''email@address.com', FirstPart, SecondPart);  
{ Results:  
    FirstPart = email  
    SecondPart = address.com  
}  
 
{ URL = https://secure.element5.com:443/register.html?productid=140005 }  
SplitStr('://', URL, Protocol, HostName, RIGHT, []);  
SplitStr('/', HostName, HostName, ObjectName, LEFT, [RIGHT]);  
SplitStr(':', HostName, HostName, PortNumber, LEFT, []);  
{ Results:  
    Protocol = "https"  
    HostName = "secure.element5.com"  
    ObjectName = "/register.html?productid=140005"  
    PortNumber = 443  
}  

Original code
procedure SplitStr(const SubStr, Str: String;  
            var LeftPart, RightPart: String;  
            MainSide: TacSplitStrSide {$IFDEF D4} = LEFT {$ENDIF};  
            LeaveSeparatorOn: TacSplitStrSides {$IFDEF D4} = [] {$ENDIF});  
var  
  I: Integer;  
begin  
  I := Pos(SubStr, Str);  
  if I <> 0 then  
   begin  
    LeftPart := Copy(Str, 1, I - 1);  
    RightPart := Copy(Str, I + Length(SubStr), Length(Str));  
 
    if LEFT in LeaveSeparatorOn then  
      LeftPart := LeftPart + SubStr;  
 
    if RIGHT in LeaveSeparatorOn then  
      RightPart := SubStr + RightPart;  
   end  
  else // if SubStr not found  
   if MainSide = LEFT then  
    begin  
     LeftPart := Str;  
     RightPart := '';  
    end  
   else  
    begin  
     LeftPart := '';  
     RightPart := Str;  
    end;  
end;  

See also
LeftPart, RightPart and SplitFileNameAndParams routines.