PROGRAM   ChSptS10 ;
{$IFDEF VER80}   USES   WinCRT , Classes ;   {$ENDIF}      {--- Delphi1 }
{$IFDEF WIN32}   {$APPTYPE CONSOLE}   USES   Classes ;   {$ENDIF}      {--- Delphi2,... }

{
!------------------------------------------------------------------------------
! This program toolbox
!------------------------------------------------------------------------------
}
{
!---------------
! TStrings routines
!---------------
! TStrings_ChSplitString
!---------------
}
{===== TStrings_ChSplitString }

PROCEDURE   TStrings_ChSplitString   ( Self : TStrings
                                     ; {$IFDEF VER80}Const{$ENDIF} I_String : String
                                     ; I_ChSeparator : Char );
VAR
   L_FldBegin : Longint ;
   L_FldAEnd  : Longint ;
BEGIN
   Self.BeginUpdate ;
   TRY
      Self.Clear ;

      L_FldBegin := 1 ;
      L_FldAEnd  := 1 ;

      {----- LOOP , Search each field }
      WHILE L_FldAEnd <= Length(I_String) DO BEGIN

         {----- If separator found... }
         IF I_String[L_FldAEnd] = I_ChSeparator
         THEN BEGIN
            {----- Add field }
            Self.Add ( Copy(I_String,L_FldBegin,L_FldAEnd-L_FldBegin) );

            {----- Prepare next search }
            Inc ( L_FldAEnd );
            L_FldBegin := L_FldAEnd ;
         END
         ELSE Inc ( L_FldAEnd );
      END;{while}

      {----- One more at the end... }
      IF I_String <> ''
      THEN Self.Add ( Copy(I_String,L_FldBegin,L_FldAEnd-L_FldBegin) );
   FINALLY
      Self.EndUpdate ;
   END;
END;


{===============}


FUNCTION   TStrings_Join   ( Self : TStrings
                           ; I_Separator : String ) : String ;
VAR
   L_i : Integer ;
BEGIN
   Result := '' ;

   {----- LOOP , Concat items }
   FOR L_i := 0 TO Self.Count -1 DO BEGIN
      IF L_i > 0 THEN Result := Result  +  I_Separator ;
      Result := Result  +  Self[L_i];
   END;{for}
END;


VAR
   G_MyStrings : TStrings ;

   G_i : Integer ;

BEGIN
   G_MyStrings := TStringList.Create ;
   TRY
      TStrings_ChSplitString ( G_MyStrings , '' , ',' );
      FOR G_i := 0 TO G_MyStrings.Count -1 DO BEGIN
         IF G_i > 0 THEN Write (   ';'   );
         Write (   G_MyStrings[G_i]   );
      END;{i}
      WriteLn (   '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , ',' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , ',,' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , 'Red,Green,Blue' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );
      WriteLn ;


      {---------------}


      TStrings_ChSplitString ( G_MyStrings , 'a' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , 'a,b' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , 'a,b,c' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , ',b,c' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , 'a,,c' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );


      TStrings_ChSplitString ( G_MyStrings , 'a,b,' , ',' );
      WriteLn (   TStrings_Join(G_MyStrings,';')  ,  '<== ( '  ,  G_MyStrings.Count  ,  '  fields )'   );
   FINALLY
      G_MyStrings.Free ;
   END;
END.
