Newsgroup: it.comp.lang.visual-basic
Titolo: Funzione Split

Salve,
è mai possibile che in vbscript esista la funzione Split x dividere una
stringa in un array in base ad un carattere di separazione, e non esiste una
funzione del genere in VB?
vi prego, ditemi che esiste.
Non sto dicendo che non è possibile implementarla, ma non vorrei fare del
lavoro inutile.

 ha scritto nel
messaggio news:9t3ag6$lbk$1...@lacerta.tiscalinet.it...


 Split(espressione[, delimitatore[, conteggio[, confronto]]])
E nel VB5 puoi usare una di queste (attenzione se OE manda a capo).
La prima è breve, la seconda è commentata:
***************************************
      Public Function Split(ByVal sIn As String, Optional sDelim As _
            String, Optional nLimit As Long = -1, Optional bCompare As _
             VbCompareMethod = vbBinaryCompare) As Variant
          Dim sRead As String, sOut() As String, nC As Integer
          If sDelim = "" Then
              Split = sIn
          End If
          sRead = ReadUntil(sIn, sDelim, bCompare)
          Do
              ReDim Preserve sOut(nC)
              sOut(nC) = sRead
              nC = nC + 1
              If nLimit <> -1 And nC >= nLimit Then Exit Do
              sRead = ReadUntil(sIn, sDelim)
          Loop While sRead <> ""
          ReDim Preserve sOut(nC)
          sOut(nC) = sIn
          Split = sOut
      End Function
***************************************
Public Function Split(ByVal Expression As String, Optional Delimiter As String =
" ", Optional Limit As _
Long = -1, Optional Compare As VbCompareMethod = vbBinaryCompare) As Variant
'===============================================================================
'   Split - This function mimics the VBA6.Split function which is not
'   available in VB5. This function helps VB5 & VB6 projects co-exist.
'
'   NOTE: Anyone making adjustments to this function should keep in
'   mind that it is intended to behave EXACTLY like the VBA6.Split
'   function (flaws and all, including operands).
'
'   WARNING: Like the VB6.Split function, do not use Split for
'   parsing a comma delimited string, as it does not conform to
'   the rules that govern the parsing of the comma delimited
'   file format.
'
'   Expression      String to parse.
'   Delimiter       Character to delimit fields.
'   Limit
'   Expression      Required. String expression containing substrings and
'                   delimiters. If expression is a zero-length string(""),
'                   Split returns an empty array, that is, an array with no
'                   elements and no data.
'   Delimiter       Optional. String character used to identify substring
'                   limits. If omitted, the space character (" ") is
'                   assumed to be the delimiter. If delimiter is a zero-length
'                   string, a single-element array containing the entire
'                   expression string is returned.
'   Limit           Optional. Number of substrings to be returned; A value
'                   of –1 (default) indicates that all substrings are returned.
'   Compare         Optional. Numeric value indicating the kind of comparison
'                   to use when evaluating substrings. See Settings section
'                   for values.
'
'   RETURNS         A Variant() array of substrings.
'===============================================================================
    Const AvgSize   As Integer = 10                                  ' fudge
factor: average field size
    Dim ArrayIncr   As Long                                          ' array
resizing increment
    Dim j           As Long                                          ' work
subscript
    Dim k           As Long                                          ' work
subscript
    Dim StrPos()    As Long                                          ' string
map: String field position
    Dim StrLen()    As Long                                          ' string
map: String field length
    Dim ExprLen     As Long                                          ' length of
the supplied string
    Dim nElements   As Long                                          ' # of
elements parsed
    Dim RetArray    As Variant                                       ' not very
efficient but the only way to RETURN a dynamic array in VB5
    Split = Empty                                                    ' by
default return a value of type Empty
    ExprLen = Len(Expression)                                        ' save this
for performance
    If ExprLen = 0 Then Exit Function                                ' exit if
nothing to parse
    If StrComp(Right$(Expression, Len(Delimiter)), Delimiter, Compare) = 0 Then
' does the string have a trailing delimiter?
        Expression = Left$(Expression, ExprLen - Len(Delimiter))     ' strip
trailing delimiter
        ExprLen = ExprLen - Len(Delimiter)                           ' track new
string length
    End If
    ArrayIncr = ExprLen / AvgSize                                    ' make a
guess of how many elements needed
    If ArrayIncr < 10 Then ArrayIncr = 20                            ' size
array at least 20 elements at a time
    Select Case Limit                                                ' a limit
has been placed on the # of elements
        Case Is > 0                                                  ' a limit
supplied
            ' all is cool
        Case 0, Is < -1                                              ' no
elements allowed
            Exit Function                                            ' we're
done
    End Select
    If Len(Delimiter) = 0 Then                                       ' delimiter
cleared
        Split = Array(Expression)                                    ' return a
single element array
    End If
    ReDim StrPos(1 To ArrayIncr)                                     '
initialize the array for looping
    ReDim StrLen(1 To ArrayIncr)                                     '
initialize the array for looping
    k = 1                                                            ' start
parsing from position 1
    j = InStr(k, Expression, Delimiter)                              ' locate
the first delimiter
    If j = 0 Then                                                    ' nothing
found
        Split = Array(Expression)                                    ' return a
single element array
        Exit Function                                                ' we're
done
    Else                                                             ' first
element found
        While j > 0                                                  ' loop
until nothing left to parse
            nElements = nElements + 1                                ' track #
of elements thus far
            StrPos(nElements) = k                                    ' track the
start of the read
            StrLen(nElements) = j - k                                ' track the
# of characters to read
            k = j + Len(Delimiter)                                   ' track
next position
            j = InStr(k, Expression, Delimiter)                      ' get next
position
            If nElements = UBound(StrPos) Then                       ' time to
resize the array
                ReDim Preserve StrPos(1 To UBound(StrPos) + ArrayIncr)    '
resize the array & make room
                ReDim Preserve StrLen(1 To UBound(StrLen) + ArrayIncr)    '
resize the array & make room
            End If
        Wend
        ReDim RetArray(nElements)                                    ' size the
array to fit
        For j = 1 To nElements                                       ' loop thru
each element
            RetArray(j) = Mid$(Expression, StrPos(j), StrLen(j))     ' copy next
substring into the array
        Next j
        Split = RetArray                                             ' return
the array
    End If
End Function
**************************
Bye, G.