Archivio

Archive for gennaio 2015

[DELPHI] – Split a string with more than one delimiter

Sometimes we need to split a string in tokens delimited by more one delimiter …

… for example, we have a string and we want to split it in tokens delimited by the characters: space, comma, greater than sign, etc.. :


String := 'EXEC SQL
                DECLARE STAFF_LIST CURSOR FOR
                     SELECT EMPID,
                            LNAME,
                            FNAME,
                            SALARY
                       FROM QMT_STAFF
                      WHERE SALARY > 10000
                   ORDER BY EMPID
           END-EXEC.'

… here’s a simple function :

// .............................................................................
//    FUNCTION : SplitStringInTokens
//               SPILT STRING IN TOKENS
//       PARMS : Source          - String to Split
//               DelimiterList   - TStringList of Delimiters
//               TokenList       - TStringList of Tokens
//      RETURN : Count of Tokens
.............................................................................
function TForm1.SplitStringInTokens(const Source: String; const DelimitersList : TStringList; var TokensList : TStringList) : Integer;
var
    SourceString    : String;
    Token           : String;
    Char            : String;
    IndexChar       : Integer;
    IndexDelimiters : Integer;
    FoundDelimiter  : Boolean;
begin

    // ... TRIM STRING
    SourceString := Trim(Source);

    IndexChar := 1;
    Token     := '';
    while ( IndexChar <= Length(SourceString) ) do begin
        // ... GET CURRENT CHAR
        Char := Copy(SourceString, IndexChar, 1);

        // ... CHECK IF THE CHAR IS A DELIMITER (SEARCH IN THE LIST)
        IndexDelimiters := 0;
        FoundDelimiter  := FALSE;
        while (FoundDelimiter = FALSE) and (IndexDelimiters < DelimitersList.Count) do begin
            if (Char = DelimitersList[IndexDelimiters]) then begin
                // ... YES, IT'S A DELIMITER! EXIT
                FoundDelimiter := TRUE;
            end
            else begin
                // ... NO, CONTINUE
                IndexDelimiters := IndexDelimiters + 1;
            end;
        end;

        // ... FOUND A DELIMITER?
        if (FoundDelimiter = TRUE) then begin
            // ... YES, SAVE TOKEN (IF NOT NULL)
            if (Token <> '') then begin
                TokensList.Add(Token);
            end;
            // ... SAVE DELIMITER (IF NOT SPACE)
            if (Char <> ' ') then begin
                TokensList.Add(Char);
            end;
            // ... COPY THE REST OF STRING TO SPLIT
            SourceString := Trim(Copy(SourceString, IndexChar+1, Length(SourceString)));
            Token        := '';
            IndexChar    := 1;
        end
        else begin
            // ... CHAR IS NOT A DELIMITER, ADD CHAR TO TOKEN
            Token := Token + Char;
            // ... INC. INDEX
            IndexChar := IndexChar + 1;
        end;
    end;
    // ... END OF STRING

    // ... ADD LAST TOKEN
    if (Token <> '') then begin
        TokensList.Add(Token);
    end;

    // ... RETURN THE COUNT OF TOKENS
    Result := TokensList.Count;
end;
//..............................................................................

… here’s how to call the function with the arguments :


...
var
    StringToSplit  : String;
    DelimitersList : TStringList;
    TokensList     : TStringList;
    i              : Integer;
begin 

    // ... CREATE THE TOKENS LIST
    TokensList      := TStringList.Create;

    // ... CREATE THE DELIMITERS LIST
    DelimitersList  := TStringList.Create;

    // ... ASSIGN THE STRING TO SPLIT
    StringToSplit   := 'EXEC SQL
                           DECLARE STAFF_LIST CURSOR FOR
                             SELECT EMPID,
                                    LNAME,
                                    FNAME,
                                    SALARY
                               FROM QMT_STAFF
                           ORDER BY EMPID
                        END-EXEC.'

    // ... ADD DELIMITERS TO THE LIST
    DelimitersList.Add(' ');
    DelimitersList.Add(',');
    DelimitersList.Add('>');
    // ... etc..

    // ... CLEAR TOKENS LIST
    TokensList.Clear;

    // ... SPLIT STRING IN TOKENS
    SplitStringInTokens(TextSource, DelimitersList, TokensList);

    // ... SHOW TOKENS LIST
    i := 0;
    While (i < TokensList.Count) do begin
        ShowMessage(TokensList[i]);
        i := i + 1;
    end;    

    // ... end  

 

 

 

… relax

Annunci