如何在InstallScript中使用多字符分隔符拆分字符串? (StrGetTokens失败)

时间:2015-12-23 17:52:37

标签: split installshield tokenize installscript

InstallScript函数StrGetTokens允许您将字符串拆分为列表。请参阅:http://helpnet.flexerasoftware.com/installshield20helplib/Subsystems/installshield20langref/Content/helplibrary/LangrefStrGetTokens.htm

这适用于单字符分隔符。此外,您可以指定“分隔符集”,即可以全部使用的1个字符分隔符的列表。没有明显的方法来指定多字符分隔符。我在尝试将我指定的字符串视为此“分隔符集”而不是分隔符本身之前尝试过,并遇到了非常意外的结果。我必须滚动自己的标记器,或导入一个,等等吗?

1 个答案:

答案 0 :(得分:0)

我在InstallScript中自己滚动。您可以使用此函数直接将调用替换为StrGetTokens。它只是将“分隔符集”参数视为多字符分隔符。我希望有更好的解决方案......

//prototype NUMBER Tokenize( BYREF LIST, STRING, STRING );
//---------------------------------------------------------------------------
//                                                                           
// Function:  Tokenize
//                                                                           
// Purpose:   Use in place of StrGetTokens when your delimiter is more than 
//            1 character in length
//
// Returns: tokenList is modifed by referrence
//      = 0     Tokenized
//      < 0 Failed to tokenize
//---------------------------------------------------------------------------
function NUMBER Tokenize( tokenList, szDelimitedValues, szDelimiter )
    STRING szReplacementToken, szReplacementTokenPlaceHolder;
    STRING svToken;
    NUMBER nListResult;
begin

    // Clear the return list
    ListDeleteAll( tokenList );

    // Return failure if attempting to tokenize an empty string
    if( szDelimitedValues = "" ) then
        return -1;
    endif;

    // When the delimiter is 0 or 1 chars, just use the native function
    if( StrLength( szDelimiter ) < 2 ) then
        return StrGetTokens( tokenList, szDelimitedValues, szDelimiter );
    endif;

    // Define a 1 character replacement delimiter, and a "placeholder" for it
    szReplacementToken            = "~";
    szReplacementTokenPlaceHolder = "[__TOKEN_PLACEHOLDER__]";

    // Replace all instances of the replacement token with its placeholder
    StrReplace( szDelimitedValues, szReplacementToken, szReplacementTokenPlaceHolder, 0 );

    // Replace all instances of the "real" delimiter with its replacment
    StrReplace( szDelimitedValues, szDelimiter, szReplacementToken, 0 );

    // Tokenize, using the replacement delimiter
    if( StrGetTokens( tokenList, szDelimitedValues, szReplacementToken ) < 0 ) then
        // If this fails, clear the tokenList and return failure
        ListDeleteAll( tokenList );
        return -1;
    endif;

    // Iterate through the token list
    nListResult = ListGetFirstString( tokenList, svToken ); 
    while( nListResult != END_OF_LIST )             

        // For each token,
        // replace all instances of the "placeholder" with the delimiter employed
        // i.e. restore the original value
        StrReplace( svToken, szReplacementTokenPlaceHolder, szReplacementToken, 0 );

        // Update the token in the list with this restored version of it
        if( ListSetCurrentString( tokenList, svToken ) < 0 ) then
            // If this fails, clear the tokenList and return failure
            ListDeleteAll( tokenList );
            return -1;
        endif;      

        nListResult = ListGetNextString( tokenList, svToken ); 
    endwhile; 

end;