Results 1 to 1 of 1

Thread: VB - Improvement to 'Split' function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    13

    VB - Improvement to 'Split' function

    Hey guys, this is my first post. I make a lot of command line apps with visual basic and I got sick of not being able to easily parse command line parameters. VB's built in Split function is nice, but it has no support for arguments in quotes (with spaces) and I really like having the application exe path as the first element in the array - like the ParamStr() function in Delphi.

    My solution was to rewrite the Split function and add support for the things I wanted. Here is the code:

    VB Code:
    1. Public Function SmartSplit(Expression As String, Delimeter As String, Optional IgnoreQuotes As Boolean, Optional AddAppPath As Boolean) As Variant
    2.     '   SmartSplit function
    3.     '   2003.10.08 by korpse
    4.     '
    5.     '   This function greatly enhances the built in 'Split' function of Visual Basic.
    6.     '   It adds support for grouping arguments that are in quotes.
    7.     '
    8.     '   This function was initially developed to split up command line arguments from Command$.
    9.     '   It also supports adding the application exe path to the first element of the array.
    10.     '   This will in essence make the command line arguments exactly like other programming
    11.     '   languages such as Delphi's built in ParamStr() function.  For example, the following:
    12.     '                   ParamStr() = SmartSplit(Command$ , " " , False, True)
    13.     '   will return the exact same thing as Delphi's ParamStr().
    14.     '
    15.     '
    16.     '   USAGE:
    17.     '
    18.     '   The parameters for SmartSplit are as follows:
    19.     '           Expression [string] = Text to be parsed.
    20.     '           Delimeter [string] = The deliminating character.  NOTE - Only supports a single character.
    21.     '           IgnoreQuotes [boolean] = If False it will group arguments that are in quotes.
    22.     '           AddAppPath [boolean] = If True it will add the application path to the first element of the returned array.
    23.     '
    24.     '   The function returns an array of a variable size, so the variable that is assigned must
    25.     '   be DIMmed as follows:
    26.     '           Dim ParamStr() as string
    27.     '   and then assigned like this:
    28.     '           ParamStr = SmartSplit(Expression , Delimeter, False, True)  'Actual parameter will differ
    29.     '
    30.     '
    31.     '   Feel free to use/post/edit this code anywhere you want, but please give me credit.  Thanks!!!
    32.        
    33.     On Error Resume Next
    34.  
    35.     Dim i As Integer, j As Integer
    36.     Dim NoQuotes As String, InQuotes As Boolean
    37.     Dim ParsedExpression(99) As String, TrimmedParsedExpression() As String
    38.     Dim ParamNumber As Integer
    39.    
    40.     'Make sure Delimeter is 1 character
    41.     If Len(Delimeter) > 1 Then Delimeter = Left$(Delimeter, 1)
    42.     If Len(Delimeter) = 0 Then Delimeter = " " 'Default to a space
    43.          
    44.     If AddAppPath = True Then 'Assign first element in array to app path.
    45.                               'This is useful when parsing Command$
    46.         ParsedExpression(0) = App.Path & "\" & App.EXEName & ".exe"
    47.         ParamNumber = 1
    48.     Else
    49.         ParamNumber = 0
    50.     End If
    51.    
    52.     'Split Expression
    53.     For i = 1 To Len(Expression)
    54.         If InQuotes = False Then
    55.             If Mid$(Expression, i, 1) = Chr$(34) And IgnoreQuotes = False Then InQuotes = True
    56.            
    57.             If Mid$(Expression, i, 1) = Delimeter Then
    58.                 'one whole parameter
    59.                 'make sure parameter is not blank - if it is ignore it
    60.                 If Trim(ParsedExpression(ParamNumber)) <> "" Then
    61.                     ParamNumber = ParamNumber + 1
    62.                     ParsedExpression(ParamNumber) = ""
    63.                 End If
    64.             Else
    65.                 ParsedExpression(ParamNumber) = ParsedExpression(ParamNumber) & Mid$(Expression, i, 1)
    66.             End If
    67.            
    68.         Else
    69.             'In quotes
    70.             If Mid$(Expression, i, 1) = Chr$(34) Then InQuotes = False
    71.             ParsedExpression(ParamNumber) = ParsedExpression(ParamNumber) & Mid$(Expression, i, 1)
    72.         End If
    73.     Next i
    74.    
    75.    
    76.     'Remove quotation marks if ignorequotes=false
    77.     If IgnoreQuotes = False Then
    78.         For i = 0 To UBound(ParsedExpression)
    79.             NoQuotes = ""
    80.             For j = 1 To Len(ParsedExpression(i))
    81.                 If Mid$(ParsedExpression(i), j, 1) <> Chr$(34) Then NoQuotes = NoQuotes & Mid$(ParsedExpression(i), j, 1)
    82.             Next j
    83.             ParsedExpression(i) = NoQuotes
    84.         Next i
    85.     End If
    86.    
    87.     'This is equivalent to a trim statement but will work with any delimeter
    88.     If ParsedExpression(ParamNumber) = "" And ParamNumber > 0 Then ParamNumber = ParamNumber - 1
    89.    
    90.     'Remove blank values from end of array
    91.     TrimmedParsedExpression = ParsedExpression()
    92.     ReDim Preserve TrimmedParsedExpression(ParamNumber)
    93.    
    94.     SmartSplit = TrimmedParsedExpression()
    95.    
    96. End Function
    Last edited by korpse; Mar 30th, 2015 at 10:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width