PROGRAM   ChSptS05 ;
{$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_String : String ;

   L_PosSep   : Longint ;
   L_i_String : String ;
BEGIN
   Self.BeginUpdate ;
   TRY
      Self.Clear ;

      L_String := I_String ;

      {----- LOOP , Search each field }
      WHILE L_String <> '' DO BEGIN

         L_PosSep := Pos ( I_ChSeparator , L_String );

         IF L_PosSep <> 0
         THEN BEGIN
            {----- Add field }
            L_i_String := Copy ( L_String , 1 , L_PosSep - 1 );
            Self.Add ( L_i_String );

            {----- Prepare next search }
            L_String := Copy ( L_String , L_PosSep + 1 , Length(L_String) - L_PosSep );
         END
         ELSE BEGIN
            {----- Add field }
            Self.Add ( L_String );

            L_String := '' ;
         END;
      END;{while}

      {----- One more at the end... }
      IF (I_String <> '') and (I_String[Length(I_String)] = I_ChSeparator)
      THEN Self.Add ( '' );
   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.
