Re: Ubound behavior with Split vs. ReDim

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 08/29/04


Date: Sat, 28 Aug 2004 21:04:09 -0400


> I have a perplexing problem in Visual Basic 6.0.
>
> I am trying to implement a Parse function that is similar to Split,
but
> which treats string elements delimited by quotes as a single item, so
that I
> can parse command lines properly.
>
> The following statement evaluates to -1:
>
> i = UBound(Split(""))
>
> The following code results in an error (Subscript out of Range):
>
> Dim Array() as String
> i = Ubound(Array)
>
> The only way that I can get Ubound to work is if I first use ReDim to
size
> my array.
>
> How can I set up a ReDim array to have no entries, as is the case with
the
> Split function?
>
> Note: It seems that I can create the same behavior if I execute the
> following statement:
>
> Dim Array() as String
> Array = Split("")

You can use the Erase statement to clear a dynamic array. I would
suggest you choose a different name for your array variable as Array is
the name of a built-in function in VB6. As for Split'ting strings where
the delimiter appears inside quote marks, you may find this function
I've posted in the past to be useful.

Function SplitAroundQuotes(TextToSplit As String, _
              Optional Delimiter As String = ",") As String()
  Dim QuoteDelimited() As String
  Dim WorkingArray() As String
  QuoteDelimited = Split(TextToSplit, """")
  For X = 1 To UBound(QuoteDelimited) Step 2
    QuoteDelimited(X) = Replace$(QuoteDelimited(X), _
                                     Delimiter, Chr$(0))
  Next
  TextToSplit = Join(QuoteDelimited, """")
  TextToSplit = Replace$(TextToSplit, Delimiter, Chr$(1))
  TextToSplit = Replace$(TextToSplit, Chr$(0), Delimiter)
  WorkingArray = Split(TextToSplit, Chr$(1))
  SplitAroundQuotes = WorkingArray
End Function

Rick - MVP



Relevant Pages

  • Re: Surprise in StrConv using vbProperCase
    ... > Your code will capitalize the first letter of a string. ... Use Split(string into array of words using blank as delimiter); ...
    (microsoft.public.access.modulesdaovba)
  • Re: Encoding Question
    ... > You receive a byte array from a socket. ... > a string with the following function, then splitting the string by the ... > delimiter sub-string. ... I have a similar problem reading in a file that can have various delimiters, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: passing a string to a C++ function
    ... identity function which adds the emails to the encryption process. ... //I set this with each item of the array as it loos and call my function ... terminated string, and it returns to me the encrypted message ... //this will ALWAYS be the delimiter ...
    (microsoft.public.vc.language)
  • Re: Surprise in StrConv using vbProperCase
    ... > Your code will capitalize the first letter of a string. ... Use Split(string into array of words using blank as delimiter); ...
    (microsoft.public.access.modulesdaovba)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)