Mega Code Archive

 
Categories / Delphi / Strings
 

Split String with custom length delimiter

Title: Split String with custom length delimiter Question: I has a string : '1000***2000***3000' or 'Aga_|_John_|_Kee_|_Lee'. I need second part of string delimited with *** or _|_ ? Answer: function GetSubstring(TempString,Delimiter : String; StrIndex : Integer) : String; var CurrIndex : Integer ; begin TempString:=TempString + Delimiter; if Pos(Delimiter,TempString)0 then begin CurrIndex:=1; while StrIndexCurrIndex do begin TempString:=Copy( TempString, Pos(Delimiter,TempString)+Length(Delimiter), MaxInt ); Inc(CurrIndex); end; Result:=Copy(TempString,0,Pos(Delimiter,TempString)-1); end; end; // --------------- Usage Example--------------------- Showmessage( GetSubString('AKA|JOHN|DUDE','|',3) ); // or Showmessage( GetSubString('AKA---JOHN---DUDE','---',3) );